使用 NodeJS 从 Firebase 实时数据库中删除多个节点

编程入门 行业动态 更新时间:2024-10-03 10:38:55

使用 NodeJS 从 Firebase 实时数据库中删除<a href=https://www.elefans.com/category/jswz/34/1771377.html style=多个节点"/>

使用 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 实时数据库中删除多个节点

本文发布于:2024-05-30 17:25:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1770744.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   节点   数据库中   实时   NodeJS

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!