javascript - 部署 Firebase 云函数时出错每个 then() 都应该返回一个值或抛出
问题描述
关于这里有什么问题的任何建议?代码来自谷歌本教程来自谷歌https://firebase.googleblog.com/2017/08/guard-your-web-content-from-abuse-with.html
尝试在 Firebase 上部署时出现此错误。
错误每个 then() 应该返回一个值或抛出 promise/always-return
这是代码:
//recaptcha
exports.checkRecaptcha = functions.https.onRequest((req, res) => {
const response = req.query.response;
console.log("recaptcha response", response);
rp({
uri: 'https://recaptcha.google.com/recaptcha/api/siteverify',
method: 'POST',
formData: {
secret: 'my_secret_key',
response: response
},
json: true
}).then(result => {
console.log("recaptcha result", result);
if (result.success) {
res.send("You're good to go, human.");
}
else {
res.send("Recaptcha verification failed. Are you a robot?");
}
}).catch(reason => {
console.log("Recaptcha request failure", reason);
res.send("Recaptcha request failed.");
});
});
非常感谢您的帮助。
解决方案
错误意味着每个.then()
方法都必须返回一些东西。
如果您不关心返回值,则可以return null;
在整个else
块之后。
在这里,您可以这样做:
if (result.success) {
return res.send("You're good to go, human.");
}
return res.send("Recaptcha verification failed. Are you a robot?");
(else
不需要,因为return
停止执行该方法,因此不会执行之后的行)
推荐阅读
- r - 将多个栅格图写入 .png
- javascript - 从文件输入中删除文件(Materialize-css)
- python - 计算滚动时间窗口的百分比变化
- python - 如何从 csv 标题行获取周数和星期几,然后附加值
- python - 无法在 jupyter notebook 中安装模块
- c# - 使用从 C# DLL 获取 PS 脚本名称?获取“对象引用未设置为对象的实例。”
- nearprotocol - 如何测试 NEAR 是否被转移?
- reactjs - 我可以通过组件列表进行映射吗?
- c# - WPF DataGrid:使用 GongSolutions.WPF.DragDrop 重新排序和编辑行
- x86 - Intel的xHC如何寻址USB设备以及如何知道设备是USB2还是USB3?