【问题标题】:Unable to update an entry in the firebase list无法更新 Firebase 列表中的条目
【发布时间】:2014-04-21 13:40:29
【问题描述】:

经过几次尝试,我仍然无法找到在 firebase 中更新列表的正确方法。

我在 firebase 中有我的开发帐户。但是,就这个问题,我使用了公共 firebase url 上的示例:

var url1= new Firebase("https://samplechat.firebaseio-demo.com/message_list");
url1.push({text:'Its working',user_id:'123'});
setTimeout(function (){
     url1.on('child_added', function(snap) {
     snap.update({text:'Its not working',user_id:'123'});
     snap.child('text').update('Its not working');
     console.log('messages in range', snap.val());} 
)},6000);

我可以设置数据,但是当消息触发了 child_add 函数时。我收到以下 javascript 错误:

Uncaught TypeError: undefined is not a function 

可能是什么问题?

【问题讨论】:

    标签: javascript firebase


    【解决方案1】:

    您没有在函数中正确使用数据快照对象。

    https://www.firebase.com/docs/javascript/datasnapshot/index.html

    改成

    snap.ref().update({text: '....', user_id: '...'});
    

    如果你只是想获得价值,你会使用

    snap.val()
    

    【讨论】:

    • 非常感谢 Mike。我挣扎了将近 2 个小时。Firebase 文档中没有明确提到这一点。
    【解决方案2】:

    请提供有关您要完成的任务的更多信息。现在你正在尝试更新你的快照(snap.update)。我不确定那是为了什么。您可以调用您的参考 url1.update,而不是调用您的 snap。另外,再看一下用于读取数据的文档,https://www.firebase.com/docs/reading-data.html 和写入数据,https://www.firebase.com/docs/writing-data.html。看起来您已经组合了读取和写入数据的功能。

    【讨论】:

    • 谢谢 Ron。我不知道快照不是反过来引用。
    猜你喜欢
    • 2018-11-26
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 2020-01-30
    • 2023-03-08
    • 1970-01-01
    • 2016-04-05
    • 1970-01-01
    相关资源
    最近更新 更多