JavaScript中的非法返回语句 [英] Illegal return statement in JavaScript
问题描述
没有删除包含1个以上孩子的天节点。我该如何解决这个问题?
A 'days' node with more than 1 child isn't getting removed. How can I fix this issue?
我需要确保我的承诺在顶层冒泡到最后一个()。所以我需要在 collectionRef.once
之前返回。但是这个return语句现在阻止了 collectionRef.once
的发生。我被困了!
I need to ensure that my promise bubbles up to the last then() on the top-level. So I need a return before collectionRef.once
. But that return statement now prevents the collectionRef.once
from happening. I'm stuck!
这是我的代码
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)!
}
})
});
});
数据结构: https://i.stack.imgur.com/gVn8S.jpg
推荐答案
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 = defaultDatabase.ref().child('/days')
// return collectionRef // ILEGAL RETURN STATEMENT
collectionRef.once('value').then(messagesData => {
console.log(`Hello messageData : ${messagesData.numChildren()}`)
if(messagesData.numChildren() > 1) {
const 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)!
}
})
})
使用 defaultDatabase.ref()。child('/ days')
而不是使用 event.data.ref.parent
也请仔细阅读文档,了解promises如何运作,它将帮助您的未来。现在这些更改都会有效。在我的结尾处进行测试。
also please go through the documentation and learn how promises works it will help you future. For now these changes will work.Tested at my end.
您必须观看的视频
event.data.ref和event.data.adminRef之间有什么区别? - #AskFirebase
使用云功能异步编程(我承诺!) Firebase - Firecasts
您可以订阅他们的 Firebase YouTube频道,以获取最新动态并了解详情。
you can subscribe their Firebase YouTube Channel to get latest updates and learn More.
这篇关于JavaScript中的非法返回语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!