I am trying to return an array of pushed data from promise, then loop multiple times to populate all data. For instance, I got a list of brands and a function to take in brand parameter and return an array of pushed data.
var datasetarr = [];
brandlist = ['Bh', 'Ruffles'];
let promiseKey = new Promise((resolve, reject) => {
for(var i = 0; i < brandlist.length; i++){
datasetarr = populateData(brandlist[i], datasetarr);
}
resolve(datasetarr);
});
promiseKey.then((arr) => {
console.log('complete promise');
for(var i = 0; i < arr.length; i++){
console.log(arr[i].date + ' ' + arr[i].total);
}
});
The error message is
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
at promiseKey.then
My data fetching has no problem as I managed to print out the details. This means that the promise is not resolve properly. Is that the correct way to return an array from promise? I not sure which part was wrong.