首页 > 解决方案 > 为什么这个 Firebase Function Promise 没有返回正确的错误?

问题描述

这是我正在编写的测试 Firebase 函数:我已经知道 ref.child(uuid) 在我的实时数据库中不存在。

ref.child(uuid).once('value')
    .then((user) => {
        if (!user.exists()) {
            console.log("Throwing, User not found")
            throw new Error("Error: No record with this UUID in Database");
        }

        //Get the node for the given provider (twitter, google or facebook)
        if (user.child(provider).exists())
            return (user.child(provider).val().status)
        else
            throw new Error("Error: Login node does not exist for this provider");
    })
    .then((result) => res.status(200).json({status: result}))
    .catch((error) => res.status(400).json({error_msg: error}));

输出

Throwing, User not found

400, {"error_msg": {}}

所以该throw new Error()行运行了,但捕获不知何故没有从“错误”参数中获取错误消息。

这怎么可能?

标签: javascriptnode.jsfirebasepromisegoogle-cloud-functions

解决方案


构造Error函数包含两个属性messagename因此更改:

{error_msg: error}

进入这个:

{error_msg: error.message}

作为参考,请检查以下内容:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error


推荐阅读