如何删除Firebase中的值 [英] How to remove values in firebase

查看:137
本文介绍了如何删除Firebase中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个小问题:
现在我有这个结构


  images 
---- uniqueId
-------- id_logement:1747657
-------- image:dataimage
---- uniqueId
-------- id_logement:1747657
-------- image:dataimage
---- uniqueId
-------- id_logement :985445234
-------- image:dataimage

更好!谢谢 !!但是:
我怎样才能删除id_logement = 1747657的所有图片?

i试过了

  firebase.database()。ref('logements /'+ key).remove(); 
firebase.database()。ref('geofire /'+ key).remove();
firebase.database()。ref('images')。child('id_logement')。equalTo(key).remove();

带有键= 1747657,但图像没有成功!这UniqueId让我紧张!请你能给我多些建议吗?非常感谢您

解决方案

由于您想基于查询批量删除数据,因此您需要先检索并删除它将其值设置为null,并使用 update 提交更改。

 让ref = firebase.database()。ref('images'); 
ref.orderByChild('id_logement')。equalTo(key).once('value',snapshot => {
let updates = {};
snapshot.forEach(child => ; updates [child.key] = null);
ref.update(updates);
});

工作 jsFiddle

Just a little question :
Now i have this structure

images
---- uniqueId
-------- id_logement : 1747657
-------- image : dataimage
---- uniqueId
-------- id_logement : 1747657
-------- image : dataimage
---- uniqueId
-------- id_logement : 985445234
-------- image : dataimage

And it's better ! Thank you !! but :
How can i remove all the images where id_logement = 1747657 ?
i tried that

        firebase.database().ref('logements/'+key).remove();
        firebase.database().ref('geofire/'+key).remove();
        firebase.database().ref('images').child('id_logement').equalTo(key).remove();

with key = 1747657 but without success for the images ! This UniqueId makes me nervous ! Please can you send me more advices ? thank you very much

解决方案

Since you want to bulk delete data based on a query, you will need to retrieve it first and delete it setting its values to null and committing the changes with update.

let ref = firebase.database().ref('images');
ref.orderByChild('id_logement').equalTo(key).once('value', snapshot => {
     let updates = {};
     snapshot.forEach(child => updates[child.key] = null);
     ref.update(updates);
});

Working jsFiddle.

这篇关于如何删除Firebase中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆