为什么此Firebase功能承诺未返回正确的错误?

红骑士91

这是我正在编写的一个测试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构造函数包含两个属性messagename因此改变:

{error_msg: error}

到这个:

{error_msg: error.message}

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

https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Error

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章