0

how can I print the "obj" value after the .on method is done with looping all items in my firebase and increments all the values? In short, what I am trying to do here is that, once the looping is done, I want to print the result ("obj") to the console.

Appreciate any help here! Thanks.

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);
nero
  • 64
  • 1
  • 8
  • See if this helps http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – elclanrs Feb 24 '14 at 08:40

2 Answers2

1

After consulting Firebase documentation, I finally found the answer for my question: https://www.firebase.com/docs/javascript/datasnapshot/index.html

Here is my updated code in case anyone face the same issue:

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

Thanks for those answers, appreciate it! :D

nero
  • 64
  • 1
  • 8
0

You have to put it inside the callback:

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

because the callback is called sometime later in the future so the only place the data is available is inside the callback or in any function that you might call from the callback.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Thanks for the answer. I have tried this method. However, the "console.log(obj)" will be **repeatedly called** whenever there is new child. I want to only execute the "console.log(obj)" **once** after all the children has been processed, any idea? :/ – nero Feb 24 '14 at 08:54
  • @nero - you will have to understand from your fbase documentation how to know when the last child has been added and how you can either know which `child_added` notification is the last one or how to get a separate notification for when it's done. You have to figure out how to know when it's done. This is asynchronous programming. It doesn't work like sequential programming. You have to put code into notification events. – jfriend00 Feb 24 '14 at 09:02
  • if you know expected number of childs, use counter and put your code inside `if (receivedCount == expectedCount) {}` – Andrey Sidorov Feb 24 '14 at 09:03