首页 > 解决方案 > 部署 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.");
    });
});

非常感谢您的帮助。

标签: javascriptfirebasegoogle-cloud-functions

解决方案


错误意味着每个.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停止执行该方法,因此不会执行之后的行)


推荐阅读