[英]How to delete a node in firebase realtime database in javascript
I'm having a difficult time deleting a node in a firebase realtime database.我很难删除 firebase 实时数据库中的一个节点。
This is what my firebase realtime database looks like:这是我的 firebase 实时数据库的样子:
This is what I tried, following delete node in firebase and How to delete/remove nodes on Firebase :这是我在 firebase 中的删除节点和如何删除/删除 Firebase 中的节点之后尝试的:
1) 1)
let chatRef = db.ref("/chats");
var query = chatRef.orderByChild("uid").equalTo(chatId);
query.on("child_added", (snapshot) => {
snapshot.ref.remove();
});
With the above code, when I clicked to delete the entire data (all of the nodes, including chat) was deleted.使用上面的代码,当我点击删除整个数据(所有节点,包括聊天)被删除。
2) 2)
chatRef.limitToLast(1).once("value", (snapshot) => {
snapshot.forEach((deedSnapshot) => {
deedSnapshot.ref.remove();
});
});
I got this working as intended, but it only removes the last node in /chats, and I want to specify the chat/uid
I want to remove.我按预期进行了此操作,但它仅删除了 /chats 中的最后一个节点,并且我想指定要删除的
chat/uid
。
3) 3)
let chatRef = db.ref("/chats");
chatRef.child("5ZuZvUyByDcbteclgpM0t08beVn1").remove();
This simply caused nothing to happen.这根本没有导致任何事情发生。 I had this in a try/catch, and clicking on "delete" led to the try running, but no errors were caught.
我在 try/catch 中有这个,点击“删除”导致尝试运行,但没有发现错误。 And nothing happened in the database.
数据库中什么也没有发生。
4) 4)
let chatRef = db.ref("/chats/MJy8cxO85ldEnDScsWZ");
chatRef.remove();
Same outcome as number 3 above -- nothing happened, and no errors were caught in the try/catch.结果与上面的第 3 点相同——什么也没发生,在 try/catch 中也没有发现错误。
UPDATE: I tried the following, but this removes the entire chats
data instead of only the node I want deleted:更新:我尝试了以下操作,但这会删除整个
chats
数据,而不仅仅是我要删除的节点:
let chatRef = db.ref("/chats");
chatRef
.orderByChild("uid")
.equalTo(chatId)
.once("value")
.then(function (snapshot) {
snapshot.forEach((childSnapshot) => {
//remove each child
chatRef.child(childSnapshot.key).remove();
});
});
Your fourth example should work, except you have a typo:你的第四个例子应该可以工作,除非你有错字:
let chatRef = db.ref("/chats/MJy8cxO85ldEnDScsWZ");
It should instead be:它应该是:
let chatRef = db.ref("/chats/-MJy8cxO85ldEnDScsWZ");
You're missing the "-" before "M".你错过了“M”之前的“-”。
I am bit late to the party.我参加聚会有点晚了。 Just like to suggest another way.
只是想建议另一种方式。
To remove a node we can pass null as a value in set
要删除节点,我们可以将 null 作为
set
的值传递
import { database } from 'firebase-admin';
const db = database();
const ref = db.ref('server/saving-data/fireblog');
dataRef.set(null, (error) => {
if (error) {
console.log('Data could not be removed.' + error);
} else {
console.log('Data removed successfully.');
}
});
To remove a node in realtime database First import remove from database删除实时数据库中的节点首先从数据库中导入删除
import { remove } from "firebase/database";
And in the delete methode you set:在您设置的删除方法中:
remove(ref(getDatabase(), `users/${userId}/node/${node.id}`))
Thanks !谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.