为什么这个Firebase函数承诺没有返回正确的错误



这是我正在编写的一个测试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"参数中获取错误消息。

这怎么可能?

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

{error_msg: error}

进入这个:

{error_msg: error.message}

参考检查如下:

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

最新更新