I Beginner in JavaScript, I’m trying to apply an If to a value that I got through fetch but gave me this error “script.js:41 Uncaught (in promise) TypeError: Cannot read property ‘substr’ of undefined at datos (script.js:41)”. I need to store in a new array only the data in base a specific date. How can I take the data from fetch and don’t have to wait for the data to be load. I hope you can understand me.
const fecha = new Date();
const today = fecha.getDate();
const year = fecha.getFullYear();
const month = fecha.getMonth() + 1;
const date = `${month}/${today}/${year}`;
fetch('./file.txt')
.then(response => response.text()
.then(data => {
let info = new Array();
info = datos(data);
}));
//Function get Array data
async function datos(data) {
let arrayLineas = new Array();
let arrayColumnas = new Array();
let arrayFiltered = new Array();
let texto = data;
arrayLineas = texto.split(/\n|\r/).splice(11);
for (let i = 0; i < arrayLineas.length; i++) {
arrayColumnas[i] = arrayLineas[i].split("^");
}
for (let x = 0; x < arrayColumnas.length; x++) {
let date_db = "";
date_db = arrayColumnas[x][17];
//console.log(date_db.substr(0,10));
//In this "If" is where the error happened
if (date === date_db.substr(0,10)) {
console.log(date);
}
}
}
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.