【发布时间】:2017-06-30 06:50:24
【问题描述】:
不会删除具有 1 个以上子节点的“天”节点。我该如何解决这个问题?
我需要确保我的承诺冒泡到顶层的最后一个 then()。所以我需要在collectionRef.once 之前返回。但是该 return 语句现在阻止了 collectionRef.once 的发生。我被困住了!
这是我的代码
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const defaultDatabase = admin.database();
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);
}).then(function() {;
const theRef = event.data.ref;
const collectionRef = theRef.parent.child('days');
return collectionRef; // ILEGAL RETURN STATEMENT
collectionRef.once('value').then(messagesData => {
if(messagesData.numChildren() > 1) {
let updates = {};
updates['/days'] = null;
return defaultDatabase.ref().update(updates); // 'days' doesn't get removed even if it has more than 1 child (as in the image)!
}
})
});
});
【问题讨论】:
-
你能不能不要重复问题,坚持一个话题,不用发同样的question
-
@MihaiIorga 没有人回答。我会接受正确的答案。
标签: javascript firebase google-cloud-functions