How to delete specific record in firebase having specified Title

时光毁灭记忆、已成空白 提交于 2021-01-27 20:30:13

问题


I was using Firebase Database in Web App, and have data like this yet:

Show Image

How can i delete the entire record having "Apple" in title (marked in picture)?

I wrote the below code but it's not working.

Kindly Help.

var abc = firebase.database().ref('firebase-test');
var key_to_delete = 'Apple';
var query = abc.orderByChild('KISNx87aYigsH3ILp0D').equalTo(key_to_delete);
query.on('child_added', function(snapshot)
{
    snapshot.ref.remove();
});

It's not giving me any error in Console.


回答1:


You're ordering/filtering on the wrong property. You have the value of he title property, so should order on that:

var abc = firebase.database().ref('firebase-test');
var key_to_delete = 'Apple';
var query = abc.orderByChild('title').equalTo(key_to_delete);
query.on('child_added', function(snapshot)
{
    snapshot.ref.remove();
});

Alternative, since you know the key of the item you want to delete, you can also delete that item without a query:

var abc = firebase.database().ref('firebase-test');
abc.child("KISNx87aYigsH3ILp0D").remove();


来源:https://stackoverflow.com/questions/46239209/how-to-delete-specific-record-in-firebase-having-specified-title

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!