【问题标题】:How to access and print a JSON object value after callback function如何在回调函数后访问和打印 JSON 对象值
【发布时间】:2014-02-24 08:37:43
【问题描述】:

如何在 .on 方法完成循环我的 firebase 中的所有项目并递增所有值后打印“obj”值?简而言之,我在这里要做的是,一旦循环完成,我想将结果(“obj”)打印到控制台。

在这里感谢任何帮助!谢谢。

var obj = {
    "20120101" : {"neutral": 0, "positive": 1, "negative": 2},
    "20120101" : {"neutral": 0, "positive": 1, "negative": 2}
} //above is just an example on the structure of the object

var fbase = new Firebase("https://<appname>.firebaseio.com/");
fbase.on('child_added', function(snapshot){
    var item = snapshot.val(); //has the attributes of: date & classification
    var date_str = item.date;
    if (!obj[date_str]){
        //initialise the counting;
        obj[date_str] = { "negative": 0, "neutral": 0, "positive" : 0 };
    }
    obj[date_str][item.classification] += 1;
});
console.log(obj);

【问题讨论】:

标签: javascript node.js firebase


【解决方案1】:

在查阅了 Firebase 文档后,我终于找到了我的问题的答案: https://www.firebase.com/docs/javascript/datasnapshot/index.html

这是我更新的代码,以防有人遇到同样的问题:

var obj = {
    "20120101" : {"neutral": 0, "positive": 1, "negative": 2},
    "20120101" : {"neutral": 0, "positive": 1, "negative": 2}
} //above is just an example on the structure of the object

var fbase = new Firebase("https://<appname>.firebaseio.com/");
fbase.once('value', function(allMessagesSnapshot) {
    allMessagesSnapshot.forEach(function(messageSnapshot) {
        var date = messageSnapshot.child('date').val();
        var classification = messageSnapshot.child('classification').val();
        // Do something with message.
        if (!obj[date]){
            obj[date] = { "negative": 0, "neutral": 0, "positive" : 0 };
        }
        obj[date][classification] += 1;
    });
    console.log(obj);
});

感谢您的回答,不胜感激! :D

【讨论】:

    【解决方案2】:

    你必须把它放在回调中:

    var obj = {
        "20120101" : {"neutral": 0, "positive": 1, "negative": 2},
        "20120101" : {"neutral": 0, "positive": 1, "negative": 2}
    } //above is just an example on the structure of the object
    
    var fbase = new Firebase("https://<appname>.firebaseio.com/");
    fbase.on('child_added', function(snapshot){
        var item = snapshot.val(); //has the attributes of: date & classification
        var date_str = item.date;
        if (!obj[date_str]){
            //initialise the counting;
            obj[date_str] = { "negative": 0, "neutral": 0, "positive" : 0 };
        }
        obj[date_str][item.classification] += 1;
        console.log(obj);
    });
    

    因为回调会在以后的某个时间调用,因此数据可用的唯一位置是回调内部或您可能从回调调用的任何函数中。

    【讨论】:

    • 感谢您的回答。我已经尝试过这种方法。但是,只要有新的孩子,“console.log(obj)”就会重复调用。我只想在处理完所有孩子之后执行“console.log(obj)”一次,知道吗? ://
    • @nero - 您必须从您的 fbase 文档中了解如何知道最后一个孩子何时添加以及如何知道哪个 child_added 通知是最后一个通知或如何获得完成时的单独通知。您必须弄清楚如何知道何时完成。这是异步编程。它不像顺序编程那样工作。您必须将代码放入通知事件中。
    • 如果您知道预期的孩子数量,请使用计数器并将您的代码放入 if (receivedCount == expectedCount) {}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多