我在firebase函数中遇到此错误日志函数已成功部署。 产生错误的代码如下所示
exports.restartAWS_Instance = functions.database.ref('stats/instances/{aws_id}')
.onUpdate((change, context) => {
var aws_instance = context.params.aws_id;
var value = change.after.val();
if(value == 1){
if (aws_instance === "reset_aws_premium"){
aws.config.update({
accessKeyId: 'XXXXXXXXXXXXXXXXXXXXX',
secretAccessKey: 'XXXXXXXXXXXXXXXXXXXXXXX',
region: 'XXXXXXXXXXXXXXXX'
});
var ec2 = new aws.EC2();
var params = {
InstanceIds: [
'XXXXXXXXXXXXXXXXXX'
]
};
console.log("Initiating server restart for aws_premium.");
return ec2.stopInstances(params, function(err, data) {
if (err) console.log(err, err.stack);
else{
console.log("aws_premium stopped.");
var at = new Date();
var time = at.setSeconds(at.getSeconds()+Number(45));
return schedule.scheduleJob("aws_premium", new Date(time), function(){
return ec2.startInstances(params, function(err, data) {
if (err) {
return false;
}
else{
schedule.cancelJob("aws_premium");
console.log("aws_premium started again.");
return true;
}
});
});
}
});
}
}
});
firebase日志显示:
12:01:55.596 PM restartAWS_Instance Function returned undefined, expected Promise or value
12:01:54.126 PM restartAWS_Instance Function returned undefined, expected Promise or value
12:01:16.985 PM restartAWS_Instance aws_free started again.
12:00:26.185 PM restartAWS_Instance aws_free stopped.
12:00:15.689 PM restartAWS_Instance Error serializing return value: TypeError: Converting circular structure to JSON
12:00:15.397 PM restartAWS_Instance Initiating server restart for aws_free
我在这里做错了什么?我错过了什么回报?
答案 0 :(得分:0)
背景中的事件触发的Firebase云端功能必须返回承诺(或在某些情况下会返回值,例如return false;
)。
我建议你观看Firebase团队的这组视频,这些视频解释了这个关键方面:
https://www.youtube.com/watch?v=7IkUgCLr5oA&t=328s
https://www.youtube.com/watch?v=652XeeKNHSk&t=308s
第一个更多是关于通过HTTP请求触发的HTTP函数(因此不是背景事件),而第二个关注于背景事件触发函数,但建议在观看第二个之前观看第一个之一。
我还建议你研究一下你会在这里找到的一些官方样本:https://github.com/firebase/functions-samples
因此,更具体地说,您应该找到让ec2.stopInstances()
方法返回承诺的方法。