使用Cloud Functions从数据库中去除价值 [英] Remove value from database using Cloud Functions
本文介绍了使用Cloud Functions从数据库中去除价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我具有以下数据结构:
如何使用Cloud Function完全删除天"?
How do I completely remove 'days' using a Cloud Function?
我当前的代码:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.deleteOldItems = functions.database.ref('/path/to/items/{pushId}')
.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);
});
functions.database.ref('/days').remove(); // <- /days doesn't get removed
});
推荐答案
您正在从函数返回,然后调用remove.试试:
You are returning from the function before calling remove. Try:
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();
});
这篇关于使用Cloud Functions从数据库中去除价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文