I encountered an issue with promise usage in JavaScript. My objective was to retrieve data from firebase, store the results in an array, and then perform sorting on that array. Below is my code snippet:
let promise = new Promise((resolve, reject) => {
var query = firebase.database().ref('');
query.once( 'value', data => {
data.forEach(subtypeSnapshot => {
var itemData = ;
var query = firebase.database().ref('').child(itemKey);
query.once( 'value', data => {
var itemDetail = ;
datasetarr.push();
});
});
resolve(datasetarr);
});
});
Upon inspecting the first console.log
inside the promise, I observed the following output:
The successful retrieval from firebase indicates no issues with that part of the code. However, when attempting to store each element in the array using the line:
datasetarr.push({type: subtype, quantity: quantity});
Upon resolving the promise and checking the items within the array, nothing gets printed out during the for loop inside .then()
. Any suggestions on how to address this?