gpt4 book ai didi

firebase - 如何删除 Firebase 列表中推送的数据?

转载 作者:行者123 更新时间:2023-12-02 22:57:39 25 4
gpt4 key购买 nike

给定

var messageListRef = new Firebase('https://SampleChat.firebaseIO-demo.com/message_list');
messageListRef.push({ 'user_id': 'fred', 'text': 'Yabba Dabba Doo!' });

如何删除添加的数据{ 'user_id': 'fred', 'text': 'Yabba Dabba Doo!' } 稍后从 Firebase?有没有一种干净简单的方法可以做到这一点?

我希望稍后能够再次找到该数据,然后将其删除,假设我不知道生成的唯一 ID,我无法执行 new Firebase('https://SampleChat.firebaseIO -demo.com/message_list/'+uniqueId).remove() (我不知道这是否是一个好的做法)。在我的想法中,我会首先查询数据,但我不知道如何使用数据列表来做到这一点。例如,我希望能够在断开连接时删除该数据。

在该页面上https://www.firebase.com/docs/web/api/firebase/push.html ,看来“查看数据列表”还没有写。路线图中是否会为数据列表添加此类删除?

最佳答案

当您调用push时,它会返回新节点。因此,您可以在内存中保留用户添加的消息列表:

var myMessageKeys = []; // put this somewhere "globally"

然后每当您添加消息时:

var newMessageRef = messageListRef.push({ 'user_id': 'fred', 'text': 'Yabba Dabba Doo!' });
myMessageKeys.push(newMessageRef.key());

就我个人而言,这感觉很奇怪。我更喜欢使用查询,例如,如果 fred 断开连接,您会执行以下操作:

var myMessages = messageListRef.orderByChild('user_id').equalTo('fred');
myMessages.on('value', function(messagesSnapshot) {
messagesSnapshot.forEach(function(messageSnapshot) {
messageSnapshot.ref().remove();
});
});

关于firebase - 如何删除 Firebase 列表中推送的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29379978/

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