NODE JS:通过allData循环时无法从Firebase实时数据库中删除数据

时间:2020-06-14 12:04:22

标签: node.js firebase firebase-realtime-database

我正在尝试通过forEach循环从Firebase中删除特定数据,但仅删除了第一项。其余的全部被跳过。我不知道这个。任何帮助表示赞赏。

这是我正在使用的整个文件,这里分为几个部分:

var membersUID = [
{ playerUid: 162847201, displayName: "Player 1", isLeader: 1, canSell: 1 },
{ playerUid: 162847202, displayName: "Player 2", isLeader: 0, canSell: 0 },
{ playerUid: 162847203, displayName: "Player 3", isLeader: 0, canSell: 0 }
];

var responseUID = 162847204;
var checkUID = Object.keys(membersUID).filter(u => membersUID[u].playerUid == responseUID)

if (checkUID) {
  console.log('responseUID is in membersUID');
} else if (!checkUID) {
  console.log('responseUID is not membersUID');
}

函数调用,通过先获取所有数据来删除数据

std::map

删除项目功能

var admin = require("firebase-admin");
var moment = require('moment');    
var serviceAccount = require("path-to-json.json");    
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "app.firebaseio.com"
});    
var db = admin.database();

导出

function removeOldData() {
    var ref = db.ref('/App/Items/');   
    var itemValue;

    ref.on("value",
        function (snapshot) {
            snapshot.forEach(element => {
                itemValue = element.val();
                if(itemValue.date === 'Invalid date' || itemValue.date === '') {
                    deleteData(element.key);
                }else {
                    date = moment(itemValue.date, "MMMM DD, YYYY").format()
                    if(moment().diff(date, 'days') > 14){
                        deleteData(element.key);
                    }
                }           
            });
        },
        function (errorObject) {
            console.log("The read failed: " + errorObject.code);
        }
   );
}

1 个答案:

答案 0 :(得分:0)

经过更多尝试和研究,使用ref.once为我解决了这个问题。