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

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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 - 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.
if you know expected number of childs, use counter and put your code inside if (receivedCount == expectedCount) {}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.