最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to delete a node in firebase realtime database in javascript - Stack Overflow

programmeradmin3浏览0评论

I'm having a difficult time deleting a node in a firebase realtime database.

This is what my firebase realtime database looks like:

This is what I tried, following delete node in firebase and How to delete/remove nodes on Firebase:

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)

      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.

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. And nothing happened in the database.

4)

      let chatRef = db.ref("/chats/MJy8cxO85ldEnDScsWZ");
      chatRef.remove();

Same oute as number 3 above -- nothing happened, and no errors were caught in the try/catch.

UPDATE: I tried the following, but this removes the entire chats data instead of only the node I want deleted:

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

I'm having a difficult time deleting a node in a firebase realtime database.

This is what my firebase realtime database looks like:

This is what I tried, following delete node in firebase and How to delete/remove nodes on Firebase:

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)

      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.

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. And nothing happened in the database.

4)

      let chatRef = db.ref("/chats/MJy8cxO85ldEnDScsWZ");
      chatRef.remove();

Same oute as number 3 above -- nothing happened, and no errors were caught in the try/catch.

UPDATE: I tried the following, but this removes the entire chats data instead of only the node I want deleted:

      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();
          });
        });
Share Improve this question edited Oct 19, 2020 at 0:34 LNA asked Oct 19, 2020 at 0:05 LNALNA 1,4473 gold badges25 silver badges37 bronze badges 2
  • Your first attempt looks about right. The only thing I'd change is to use .once("value", ...) instead – Phil Commented Oct 19, 2020 at 0:31
  • Thanks Phil! I found stackoverflow./questions/40441625/… and tried my first attempt again, rewriting it slightly: ``` 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(); }); }); ``` However, this removes the entire data inside chats instead of the single node. :/ – LNA Commented Oct 19, 2020 at 0:33
Add a ment  | 

3 Answers 3

Reset to default 6

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".

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 !

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

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.');
      }
    });
发布评论

评论列表(0)

  1. 暂无评论