Javascript变量是一个对象数组,但无法访问元素

时间:2016-08-24 09:39:07

标签: javascript firebase firebase-realtime-database

我正在使用Firebase数据库和Javascript,我有代码可以获得每个类别中的每个问题。我有一个名为category的对象将包含名称,问题和问题计数,然后它将被推入类别列表(questionsPerCategory)。在回调函数内部,我只做console.log(questionsPerCategory)。它打印包含类别和问题的对象(数组)。现在我的问题是当我console.log(questionsPerCategory[0])表示它是未定义时,我也尝试了console.log(questionsPerCategory.pop()),因为它是一个数组,但它也是 undefined 。这是为什么?下面是控制台日志的代码和图像。附加说明:CODE A和C是异步的,CODE B和D是同步的。

enter image description here

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');

    // CODE A
    ref.once("value", function(snapshot) {

        // CODE B
        snapshot.forEach(function(childSnapshot) {
            var key = childSnapshot.key;

            var childData = childSnapshot.val();

            var category = {
                category_name: childData.category_name
            };

            // CODE C               
            questionRef.orderByChild("category_name").equalTo(childData.category_name).once("value", function(questionSnapshot){
                var count = 0;
                var q = [];

                // CODE D
                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); 

    });
};

2 个答案:

答案 0 :(得分:2)

当所有 async 调用完成后,callback(questionsPerCategory);应该会发生。

现在,当调用回调时,questionsPerCategory尚未就绪。 我会用Promise API来完成这个。

根据您使用的Promise库,这可以通过不同的方式完成,例如:使用bluebird看起来您需要map功能。

答案 1 :(得分:0)

试试这段代码:

this.getQuestionsForEachCategory = function(callback, questions) {
var ref = firebase.database().ref('category');
var questionRef = firebase.database().ref('question');
console.log('get questions for each category');
var questionsPerCategory = [];
// CODE A
ref.once("value", function(snapshot) {

    // CODE B
    snapshot.forEach(function(childSnapshot) {
        var key = childSnapshot.key;

        var childData = childSnapshot.val();

        var category = {
            category_name: childData.category_name
        };

        // CODE C               
        questionRef.orderByChild("category_name").equalTo(childData.category_name).once("value", function(questionSnapshot){
            var count = 0;
            var q = [];

            // CODE D
            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); 
    });
});
};