0

I just started to use Firebase with JavaScript, I've read the doc on Firebase's site, but it talks just about the immediate reference of a node which invokes on('child_added',function(){}).

I'm trying to find a particular node to listen for. But it seems that it can't invoke the handler startListening when data is changed.

Here is my code

            var myFire = new Firebase('https://myProject.firebaseio.com/chat');
            var myConversation = "notAssigned"; 
            myFire.once("value", function(snapshot) {
                        snapshot.forEach(function(childSnapshot) {
                         childSnapshot.forEach(function(i){
                            var user = i.val();
                            if (user == "10") myConversation = childSnapshot.child("messages").ref();
                            });
                        });
                });
            var textInput = document.querySelector('#text');
            var postButton = document.querySelector('#post');

              postButton.addEventListener("click", function() {
              var msgUser = "10";
              var msgText = textInput.value;
              var date = js_dd_mm_yyyy_hh_mm_ss (); //function that returns actual date
              myConversation.push({content:msgText,date:date, status:"1", type:"text", user:msgUser});
                textInput.value = "";
            });


            /** Function to add a data listener **/
        alert (myConversation.key());
        var startListening = function() {
                myConversation.on('child_added', function(snapshot) {
                        alert("here i am");
                        alert (snapshot.content);
              });
            }

            // Begin listening for data
            startListening();

I think that I fail to manage references, although when I invoke push data is actually loaded, that is, when I look at Database in Firebase's console, the push method has successful pushed new node.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Luigi Blu
  • 129
  • 13
  • If you just started using Firebase, you should start from [firebase.google.com/docs](https://firebase.google.com/docs/). You now seem to be using code from firebase.com (e.g. `new Firebase()`), which is deprecated. – Frank van Puffelen Oct 09 '16 at 17:49
  • The most likely cause of the problem is that you're not signed in and your data is only accessible to authenticated users. See [this SO docs page](http://stackoverflow.com/documentation/firebase/5548/how-do-i-listen-for-errors-when-accessing-the-database/23610/detecting-errors-when-reading-data-in-javascript#t=201610091748406071391) for how to detect that and the first blue Info box on [this Firebase docs page](https://firebase.google.com/docs/database/android/read-and-write) for how to fix it. – Frank van Puffelen Oct 09 '16 at 17:49
  • I've discovered the reason why, my function inside once("value",...) doesn't alter the myConversation value. Can you help me understand why the internal scope does not change the value of the external variable? – Luigi Blu Oct 09 '16 at 18:17
  • It *does* modify the external variable. But it does it later than you think. See http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Frank van Puffelen Oct 09 '16 at 19:02

0 Answers0