Javascript Firebase云函数Cron更新

Javascript Firebase云函数Cron更新,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,我有一个函数,用于根据Cron作业更新数据库。看起来是这样的(值得一提的是,我在这里得到了很多帮助) exports.minute\u作业= functions.pubsub.topic('minute-tick').onPublish((event)=>{ var ref=admin.database().ref(“注释”) 参考一次(“值”)。然后((快照)=>{ var更新={}; snapshot.forEach(commentSnapshot=>{ var comment=commen

我有一个函数,用于根据Cron作业更新数据库。看起来是这样的(值得一提的是,我在这里得到了很多帮助)

exports.minute\u作业=
functions.pubsub.topic('minute-tick').onPublish((event)=>{
var ref=admin.database().ref(“注释”)
参考一次(“值”)。然后((快照)=>{
var更新={};
snapshot.forEach(commentSnapshot=>{
var comment=commentSnapshot.val();
var currentRating=comment.rating-comment.lastRating;
var newScore=((数学.abs(评论.internalScore)*0.95)+当前评级)*-1;
如果(newScore<0.000001){newScore=0.000001}
更新[commentSnapshot.key+“/lastRating”]=comment.rating;
更新[commentSnapshot.key+“/internalScore”]=新闻核心;
});
参考更新(更新);
})
});
除了我从Firebase日志中得到以下警告外,所有这些都工作正常:

函数返回未定义的预期承诺或值


感谢您的帮助

因为您的云函数没有返回值,所以Google云函数引擎不知道代码何时完成。在许多情况下,这意味着GCF将在关闭
}
执行后立即终止函数的包含。但此时,您的代码可能仍在从数据库加载数据,而且它肯定还没有更新数据库

解决方案是返回一个promise,它只是一个对象,当您处理完数据库时,它将发出信号。好消息是,
once()
update()
都已经返回了承诺,所以您可以返回这些承诺:

exports.minute_job =
  functions.pubsub.topic('minute-tick').onPublish((event) => { 

    var ref = admin.database().ref("comments")
    return ref.once("value").then((snapshot) => {
        var updates = {};
        snapshot.forEach(commentSnapshot => {
            var comment = commentSnapshot.val();

            var currentRating = comment.rating - comment.lastRating;          
            var newScore = ((Math.abs(comment.internalScore) * 0.95) + currentRating) * -1;

            if(newScore < 0.000001) { newScore = 0.000001}

            updates[commentSnapshot.key + "/lastRating"] = comment.rating;
            updates[commentSnapshot.key + "/internalScore"] = newScore;         
        });

        return ref.update(updates);
    })
  });
exports.minute\u作业=
functions.pubsub.topic('minute-tick').onPublish((event)=>{
var ref=admin.database().ref(“注释”)
返回ref.one(“值”)。然后((快照)=>{
var更新={};
snapshot.forEach(commentSnapshot=>{
var comment=commentSnapshot.val();
var currentRating=comment.rating-comment.lastRating;
var newScore=((数学.abs(评论.internalScore)*0.95)+当前评级)*-1;
如果(newScore<0.000001){newScore=0.000001}
更新[commentSnapshot.key+“/lastRating”]=comment.rating;
更新[commentSnapshot.key+“/internalScore”]=新闻核心;
});
返回参考更新(更新);
})
});

现在谷歌云函数知道你的代码在
}
之后仍然有效,因为你返回了一个承诺。当你的
update()
完成时,它会解决它返回的承诺,谷歌云函数可以关闭容器(或者至少:停止向你收取使用费)。

由于你的云函数不返回值,谷歌云函数引擎不知道代码何时完成。在许多情况下,这意味着GCF将在关闭
}
执行后立即终止函数的包含。但此时,您的代码可能仍在从数据库加载数据,而且它肯定还没有更新数据库

解决方案是返回一个promise,它只是一个对象,当您处理完数据库时,它将发出信号。好消息是,
once()
update()
都已经返回了承诺,所以您可以返回这些承诺:

exports.minute_job =
  functions.pubsub.topic('minute-tick').onPublish((event) => { 

    var ref = admin.database().ref("comments")
    return ref.once("value").then((snapshot) => {
        var updates = {};
        snapshot.forEach(commentSnapshot => {
            var comment = commentSnapshot.val();

            var currentRating = comment.rating - comment.lastRating;          
            var newScore = ((Math.abs(comment.internalScore) * 0.95) + currentRating) * -1;

            if(newScore < 0.000001) { newScore = 0.000001}

            updates[commentSnapshot.key + "/lastRating"] = comment.rating;
            updates[commentSnapshot.key + "/internalScore"] = newScore;         
        });

        return ref.update(updates);
    })
  });
exports.minute\u作业=
functions.pubsub.topic('minute-tick').onPublish((event)=>{
var ref=admin.database().ref(“注释”)
返回ref.one(“值”)。然后((快照)=>{
var更新={};
snapshot.forEach(commentSnapshot=>{
var comment=commentSnapshot.val();
var currentRating=comment.rating-comment.lastRating;
var newScore=((数学.abs(评论.internalScore)*0.95)+当前评级)*-1;
如果(newScore<0.000001){newScore=0.000001}
更新[commentSnapshot.key+“/lastRating”]=comment.rating;
更新[commentSnapshot.key+“/internalScore”]=新闻核心;
});
返回参考更新(更新);
})
});

现在谷歌云函数知道你的代码在
}
之后仍然有效,因为你返回了一个承诺。当你的
update()
完成后,它会解决它返回的承诺,谷歌云功能可以关闭容器(或者至少:停止向你收取使用费)。

再次感谢弗兰克,没有你,我真的不会把这些放在一起。看来一切都很好,非常感谢。再次感谢弗兰克,没有你,我真的不会把这一切都安排好。看来一切都很好,非常感谢。