Javascript变量是对象数组,但无法访问元素 [英] Javascript variable is an array of objects but can't access the elements

查看:64
本文介绍了Javascript变量是对象数组,但无法访问元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using Firebase database and Javascript, and I have code that will get each question in each category. I have an object called category will contain the name, the questions, and the question count, then it will be pushed into the list of categories (questionsPerCategory). Inside the the callback function I just do console.log(questionsPerCategory). It prints the object (array) that contains the categories and questions. Now my problem is that when I do console.log(questionsPerCategory[0]) is says it's undefined, I also tried console.log(questionsPerCategory.pop()) since it's an array but it's also undefined. Why is that? Below is the code and the 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');

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

    });
};

推荐答案

callback(questionsPerCategory);应该在所有 async 调用结束时发生.

The callback(questionsPerCategory); should happen when all async calls are finished.

现在,调用回调questionsPerCategory尚未准备就绪. 我会使用Promise API完成此操作.

Right now the questionsPerCategory is not ready when the callback is called. I would use Promise API to accomplish this.

根据您使用的Promise库,这可以通过不同的方式来实现,例如通过使用 bluebird 看来您需要

Depending on the Promise library you are using, this can be accomplished in a different ways, e.g. by using bluebird it looks like you need map functionality.

这篇关于Javascript变量是对象数组,但无法访问元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆