Reputation: 877
I am working in firebase for a web app. The first time I did the query it worked, then I added the second reference and it broke. Upon doing that, I rolled back to when it previously worked and it no longer worked either. I am getting no errors in the console.
function displayMessage(){
var messageId;
alert("Firebase Initializing UserID: " + Parse.User.current().id);
var reference = new Firebase("https://gibber.firebaseio.com/Recent");
reference.orderByChild("userId").equalTo(Parse.User.current().id).on("child_added", function(snapshot) {
alert(snapshot.key());
messageId = snapshot.key();
});
var reference2 = new Firebase("https://gibber.firebaseio.com/Recent");
reference2.child(messageId+"/lastMessage").on("value", function(snapshot) {
alert(snapshot.val());///snapshot should be last message
});
};
Upvotes: 0
Views: 38
Reputation: 599956
The nesting of you code is off, which is hard to spot because the indentation is messy.
This works:
var userId = 'sKdkC6sGYb';
var ref = new Firebase("https://gibber.firebaseio.com/Recent");
ref.orderByChild("userId").equalTo(userId).on("child_added", function(snapshot) {
console.log('child_added: '+snapshot.key());
var messageId = snapshot.key();
ref.child(messageId+"/lastMessage").on("value", function(othersnapshot) {
console.log('value: '+othersnapshot.val());///snapshot should be last message
});
});
I'm not sure why you're taking this two-queried approach though, the lastMessage
property is available in the outer snapshot already:
var userId = 'sKdkC6sGYb';
var ref = new Firebase("https://gibber.firebaseio.com/Recent");
ref.orderByChild("userId").equalTo(userId).on("child_added", function(snapshot) {
console.log('child_added: '+snapshot.key());
console.log('value: '+snapshot.val().lastMessage);
});
See: http://jsbin.com/bikoni/edit?js,console
Upvotes: 2