I use a Firebase and Javascript database, and I have code that will receive every question in each category. I have an object called category, which will contain the name, questions and the number of questions, then it will move to the list of categories (questionsPerCategory). Inside the callback function, I just do console.log(questionsPerCategory). It prints an object (array) containing categories and questions. Now my problem is that when I do console.log(questionsPerCategory[0])it says undefined, I also tried console.log(questionsPerCategory.pop()), since it is an array, but also undefined. Why is this? Below is the code and image of the console log. Additional Note: CODE A and C are asynchronous, CODE B and D are synchronous.

this.getQuestionsForEachCategory = function(callback, questions, questionsPerCategory) {
var ref = firebase.database().ref('category');
var questionRef = firebase.database().ref('question');
console.log('get questions for each category');
ref.once("value", function(snapshot) {
snapshot.forEach(function(childSnapshot) {
var key = childSnapshot.key;
var childData = childSnapshot.val();
var category = {
category_name: childData.category_name
};
questionRef.orderByChild("category_name").equalTo(childData.category_name).once("value", function(questionSnapshot){
var count = 0;
var q = [];
questionSnapshot.forEach(function(childQuestionSnapshot) {
var questionObj = childQuestionSnapshot.val();
count++;
questions.push(questionObj.question);
q.push(questionObj.question);
});
category.questions = q;
category.questionCount = count;
questionsPerCategory.push(category);
});
});
callback(questionsPerCategory);
});
};