调用.remove()上的Firebase调用将删除所有父母 [英] Firebase calling .remove() on ref is removing all parents
问题描述
var Firebase = require('firebase');
var config = require('./ config.json');
var fbRef = new Firebase(config.firebaseEndpoint +/ messages);
var messenger = require('./ messenger.js');
$ b fbRef.on('value',function(snap){
//如果返回数据,则将有效载荷转换为数组
//将每个窗口messenger,完成后删除
// firebase记录
if(snap.exists()){
var payload = snap.val();
var key = Object.keys(payload);
keys.forEach(function(key){
var msg = payload [key];
messenger (msg,function(msgErr,msgData){
if(!msgErr){
fbRef.child(key).remove();
}
else {
console.log(msgErr);
}
});
});
}
});
提示:以文字形式将其添加到您的问题中),但我认为您可能错误地理解了Firebase的运作方式。
Firebase存储路径标识的位置值。在某个位置存储值时,将自动创建路径。当你从位置中删除最后一个值时,路径被自动删除。
除此之外,这段代码更具惯用性:
fbRef.on('value',function(snap){
if(snap.exists()){
snap.forEach函数(msgSnap){
var msg = msgSnap.val();
messenger(msg,function(msgErr,msgData){
if(!msgErr){
msgSnap.ref ();
}
else {
console.log(msgErr);
}
});
});
}
});
In the following code, after processing the data returned in the snapshot, I am seeking to remove the record in question. When I call remove on my reference with the key of the record appended via the child method all of the parent nodes are removed up to the root of the FB app. I suspect it is an error in my code. Would appreciate any assistance that can be provided.
var Firebase = require('firebase');
var config = require('./config.json');
var fbRef = new Firebase(config.firebaseEndpoint + "/messages");
var messenger = require('./messenger.js');
fbRef.on('value', function (snap) {
//If Data is returned convert payload to array.
//pass each mesaage into messenger and upon completion delete the
// firebase record
if (snap.exists()) {
var payload = snap.val();
var keys = Object.keys(payload);
keys.forEach(function (key) {
var msg = payload[key];
messenger(msg, function (msgErr, msgData) {
if (!msgErr) {
fbRef.child(key).remove();
}
else {
console.log(msgErr);
}
});
});
}
});
It's hard to be certain without seeing your JSON structure (hint: add it to your question as text), but I think you may be mistaking how Firebase operates.
Firebase stores values at locations identified by paths. When you store a value at a location, the path is automatically created. When you remove the last value from a location, the path is automatically removed.
Aside from that, this code is somewhat more idiomatic:
fbRef.on('value', function (snap) {
if (snap.exists()) {
snap.forEach(function(msgSnap) {
var msg = msgSnap.val();
messenger(msg, function (msgErr, msgData) {
if (!msgErr) {
msgSnap.ref().remove();
}
else {
console.log(msgErr);
}
});
});
}
});
这篇关于调用.remove()上的Firebase调用将删除所有父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!