gpt4 book ai didi

javascript - Node 没有从 Firebase 数据库中删除?

转载 作者:行者123 更新时间:2023-11-30 15:14:59 25 4
gpt4 key购买 nike

我有以下数据结构:

enter image description here

不幸的是,'days' 不会通过下面的代码从数据库中删除。

我当前的代码:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}') //irrelevant to the question
.onWrite(event => {
var ref = event.data.ref.parent; // reference to the items
var now = Date.now();
var cutoff = now - 2 * 60 * 60 * 1000;
var oldItemsQuery = ref.orderByChild('timestamp').endAt(cutoff);

return oldItemsQuery.once('value', function(snapshot) {
// create a map with all children that need to be removed
var updates = {};
snapshot.forEach(function(child) {
updates[child.key] = null
});
// execute all updates in one go and return the result to end the function
return ref.update(updates);
}).then(function() {;
return functions.database.ref('/days').remove(); // /days doesn't get removed!
});

});

最佳答案

你可以这样做:

let updates = {};
updates['/days'] = null;

firebase.database().ref().update(updates);

参见 docs 中的“更新或删除数据” ;

在您的代码中:

.then(function() {;
let updates = {};
updates['/days'] = null;

return firebase.database().ref().update(updates)
});

编辑:尝试这样的事情:

const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const defaultDatabase = admin.database();

...

let updates = {};
updates['/days'] = null;
defaultDatabase.ref().update(updates);

引用:https://firebase.google.com/docs/database/admin/start

关于javascript - Node 没有从 Firebase 数据库中删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44603121/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com