简体   繁体   English

如何删除firebase实时数据库中的一个节点javascript

[英]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.

相关问题 如何用key删除firebase实时数据库子节点? - how to delete firebase realtime database child node with key? 如何从firebase实时数据库中删除选中的item节点? - How to delete selected item node from firebase realtime database? 如何删除Firebase实时数据库中的特定数据? - How to delete a specific data in Firebase Realtime Database? 如何从 Firebase 实时数据库中删除一些数据? - How can I delete some data from Firebase Realtime Database? 对于或同时使用 Javascript 和 Firebase 实时数据库 - For or While with Javascript and Firebase Realtime Database firebase 从实时数据库中删除特定项目 - firebase delete specific item from Realtime Database 如何更新 firebase 实时数据库 (JavaScript) 中键的值 - How to update the value of a key in firebase Realtime database (JavaScript) 使用 android studio java Recycler 视图从 Firebase 实时数据库中删除一个节点 - Delete a node from Firebase realtime database with android studio java Recycler view 如何使用 javascript 在 firebase 实时数据库中使用自定义 uid 进行身份验证? - how to authenticate with custom uid in firebase realtime database using javascript? 如何在 firebase 实时数据库 javascript 中用新数据添加以前的数据 - How to add previous data with new one in firebase realtime database javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM