我在 Firebase 中设置了以下数据库:
我正在尝试编写一个脚本,该脚本在媒体内(在所有用户中)进行搜索,并在任何特定媒体 url 与我传递的 url 参数匹配时返回匹配项。如果返回匹配项,我想删除此媒体 key /节点。但是,我的脚本只是返回 null。有任何想法吗?
const ref = firebase.database().ref('/users/');
const url = 'https://XXXXXX.s3.amazonaws.com/profiles/william/1478471907-me.jpg';
return ref.child('media').orderByChild('url').equalTo(url).once("value").then(function(snapshot) {
console.log(JSON.stringify(snapshot.val()));
});
最佳答案
好的,感谢 Imjared 的反馈,我找到了一个解决方案 - 包括删除节点的方法:
// reference the media, B4K... could be an variable to target the correct key
const ref = firebase.database().ref('/users/B4KWeemv78R2GP7Jqul2f70kVM73/media');
return ref.orderByChild('url').equalTo(url).once("value").then(function(snapshot) {
// get the key of the respective image
const key = Object.keys(snapshot.val())[0];
// delete image node from firebase
ref.child(key).remove();
return {
success: 1,
message: 'Image deleted.'
};
}
关于javascript - 如何在Firebase数据库中查询子节点然后删除节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40494358/