I am pulling data from Firebase. I want to extract values and save them in the array. After that I will use that array values as strings in my application. The problem is when I push values from DB to array, it adds undefined at the end of the array. And when I want to see the value of array (carUserDetails[0]) it returns undefined, but should return a string "BMW"
var UserID = firebase.auth().currentUser.uid;
var refModels = firebase.database().ref("/users/" + UserID);
var carUserDetails = [];
refModels.once('value').then(function (snapshot) {
snapshot.forEach(function (childSnapshot) {
var usersValue = childSnapshot.val(); //get the values of the object
carUserDetails.push(
usersValue.carMake,
usersValue.carYear,
usersValue.carModel,
usersValue.carTrim
);
});
});
console.log(carUserDetails);
console.log(carUserDetails[0]) // returns undefined too
So what could be the problem