【问题标题】:Why is this Firebase Function Promise not returning the correct Error?为什么这个 Firebase Function Promise 没有返回正确的错误?
【发布时间】:2020-03-13 18:52:28
【问题描述】:

这是我正在编写的测试 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() 行运行了,但是catch 不知何故没有从'error' 参数中获取错误消息。

这怎么可能?

【问题讨论】:

    标签: javascript node.js firebase promise google-cloud-functions


    【解决方案1】:

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

    {error_msg: error}
    

    进入这个:

    {error_msg: error.message}
    

    参考如下:

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

    【讨论】:

    • 值得注意的是,这是因为 Error 对象的各种属性不可枚举,因此被 JSON.stringify(someErrorObject) 忽略。如果您想了解更多信息,this question 会就该主题以及如何绕过它进行一些有趣的讨论。
    猜你喜欢
    • 2021-01-11
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多