多个节点"/>
使用 NodeJS 从 Firebase 实时数据库中删除多个节点
我有以下代码:
poolPlayersRef.child(currPool).once('value', function(playerSnapshot) {
// Iterate to get all the players
playerSnapshot.forEach(function(childSnap) {
playersInPoolRef.child(childSnap.key).child(currPool).remove().then(function() {
// 1. Display confirmation
// 2. Now remove from the pool player list as well poolPlayersRef.child(currPool).child(childSnap.key).remove().then(function() {
// Display confirmation
}).catch(function(error) {
console.log("PoolPlayersRef Remove Failed:" + error.message);
});
}).catch(function(error) {
console.log("PlayersInPoolRef Remove Failed:" + error.message);
});
});
}).then(function() {
// Now remove from the pool player list as well
poolsRef.child(currPool).remove().then(function() {
//DISPLAY CONFIRMATION MESSAGE
}).catch(function(error) {
console.log("DELETE POOL Failed:" + error.message);
});
}).catch(function(error) {
console.log("PoolPlayersRef Node Read Failed:" + error.message);
});
我希望它从 poolPlayersRef 节点读取并为每个玩家删除条目。然后也删除该节点中关联的 playersInPoolRef 条目。
一旦所有孩子都被删除,然后删除 poolRef.
我的代码的工作原理是,它会一个接一个地删除 PoolPlayersRef 和 PoolPlayersRef 中的玩家,但是 Pool 的删除会立即发生,我更希望它只在上述节点中的所有条目完成后才被删除。
有人可以推荐如何修复我的代码!?
回答如下:一种方法是收集
remove
调用返回的承诺,然后在 Promise.all
中返回所有承诺:
poolPlayersRef.child(currPool).once('value', function(playerSnapshot) {
let promises = p[]
playerSnapshot.forEach(function(childSnap) {
promises.push(playersInPoolRef.child(childSnap.key).child(currPool).remove());
});
return Promise.all(promises);
}).then(function() {
return poolsRef.child(currPool).remove();
}).catch(function(error) {
console.log("PoolPlayersRef Node Read Failed:" + error.message);
});
更多推荐
使用 NodeJS 从 Firebase 实时数据库中删除多个节点
发布评论