gpt4 book ai didi

javascript - 我只得到一次数据库输出,但没有再得到一次。为什么这不起作用?

转载 作者:行者123 更新时间:2023-12-02 15:02:18 26 4
gpt4 key购买 nike

我正在 firebase 中开发一个网络应用程序。我第一次执行查询时它起作用了,然后我添加了第二个引用,它就崩溃了。这样做后,我回滚到之前有效的时间,但它也不再有效了。我在控制台中没有收到任何错误。

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

enter image description here

最佳答案

代码的嵌套已关闭,很难发现,因为缩进很困惑。

这有效:

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

我不确定为什么要采用这种两次查询的方法,lastMessage 属性已经在外部快照中可用:

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

参见:http://jsbin.com/bikoni/edit?js,console

关于javascript - 我只得到一次数据库输出,但没有再得到一次。为什么这不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35347780/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com