英文:
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);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论