How do I set the snapshot value to a $scope variable

397 views
Skip to first unread message

chris...@cwtinteractive.com

unread,
Oct 6, 2016, 4:04:12 AM10/6/16
to Firebase Google Group
// Books Controller
var databaseRef = firebase.database().ref('books');

$scope
.allBooks = function() {
    bookRef.on('value', function(snapshot) {
        var data = snapshot.val();

        return data;
    }

};



I am trying to set the $scope.allBooks variable to an array of objects containing all of the books in the 'books' node. Can anyone help me with this?

Kato Richardson

unread,
Oct 6, 2016, 11:14:09 AM10/6/16
to Firebase Google Group
You can't return a value from an asynchronous method. Read more here.

Considering that you're struggling with asynchronous concepts here, the best option might be to use a library like AngularFire (for Angular 1.x) or AngularFire2 (for Angular 2.x) to manage these things.

If you're determined to try this out yourself, the Stack Overflow link above on async should be considered required reading, and probably contains enough examples to help you through this.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-talk+unsubscribe@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/c7d19a8c-9e6f-424a-b379-0ae7e986d5d1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Reply all
Reply to author
Forward
0 new messages