从Firebase实时数据库中删除数据会删除父节点中的所有数据。

huangapple go评论67阅读模式
英文:

Removing data from Firebase Realtime Database removes all the data in the parent node

问题

我正在使用Firebase编写代码来从实时数据库中删除数据。
我试图做的是,在单击按钮时从用户的书签部分中删除保存的帖子,以下是代码的关键部分:

let user = firebase.auth().currentUser;
var ref1 = firebase.database().ref('saved/' + user.uid + '/posts/').orderByChild('postnum').equalTo(j); //j是要删除的帖子的postnum
ref1.once('value', function(snapshot){
    snapshot.ref.remove();
});

而不是删除特定帖子的信息,该代码会清除用户之前保存的所有帖子。换句话说,它会清除'saved/user.uid/posts/'目录中的所有数据。

我做错了什么?

英文:

I am using Firebase and am writing code to delete data from Realtime Database.
What I am trying to do is to remove a saved post from a user's bookmarks section when the button is clicked, and here is the key part of the code:

let user=firebase.auth().currentUser;
var ref1=firebase.database().ref('saved/'+user.uid+'/posts/').orderByChild('postnum').equalTo(j); //j is the postnum of the post to be deleted
ref1.once('value',function(snapshot){
    snapshot.ref.remove();
});

Instead of removing the info about the specific post, the code would clear all the posts that the user previously saved. (i.e. this would clear all data from 'saved/user.uid/posts/' directory.

What am I doing wrong?

答案1

得分: 0

当您执行对 Firebase 数据库的查询时,可能会有多个结果。因此,快照包含了这些结果的列表。即使只有一个结果,快照也会包含一个结果的列表。

因此,snapshot 变量包含了结果的列表。而 snapshot.ref 是指您运行查询的位置。所以当您执行 snapshot.ref.delete() 时,您删除的是运行查询的整个位置,而不仅仅是结果。

要删除结果,循环遍历快照的子项,逐个删除它们:

let user = firebase.auth().currentUser;
var ref1 = firebase.database().ref('saved/' + user.uid + '/posts/').orderByChild('postnum').equalTo(j); // j 是要删除的帖子的 postnum
ref1.once('value', function(snapshot) {
  snapshot.forEach(function(child) {
    child.ref.remove();
  });
});

您也可以在循环之后使用多位置更新一次性删除它们:

let user = firebase.auth().currentUser;
var ref1 = firebase.database().ref('saved/' + user.uid + '/posts/').orderByChild('postnum').equalTo(j); // j 是要删除的帖子的 postnum
ref1.once('value', function(snapshot) {
  let updates = [];
  snapshot.forEach(function(child) {
    updates[child.key] = null;
  });
  ref1.update(updates);
});
英文:

When you execute a query against the Firebase Database, there will potentially be multiple results. So the snapshot contains a list of those results. Even if there is only a single result, the snapshot will contain a list of one result.

So the snapshot variable contains a list of result. And snapshot.ref refers to the location where you ran the query. So when you do snapshot.ref.delete(), you're deleting the entire location where you ran the query, not just the results.

To delete the results, loop over the children of snapshot, and delete them one by one:

let user=firebase.auth().currentUser;
var ref1=firebase.database().ref('saved/'+user.uid+'/posts/').orderByChild('postnum').equalTo(j); //j is the postnum of the post to be deleted
ref1.once('value',function(snapshot){
  snapshot.forEach(function(child) {
    child.ref.remove();
  });
});

You can also delete them in one go after the loop with a multi-location update:

let user=firebase.auth().currentUser;
var ref1=firebase.database().ref('saved/'+user.uid+'/posts/').orderByChild('postnum').equalTo(j); //j is the postnum of the post to be deleted
ref1.once('value',function(snapshot){
  let updates = [];
  snapshot.forEach(function(child) {
    updates[child.key] = null;
  });
  ref1.update(updates);
});

huangapple
  • 本文由 发表于 2020年1月6日 22:52:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定