Javascript 试图理解Mongoose save()方法
阅读我看到的文件 Mongoose异步操作,如.save()和查询,返回ES6承诺 我认为我熟悉ES6承诺,所以我写了如下内容:Javascript 试图理解Mongoose save()方法,javascript,mongoose,es6-promise,Javascript,Mongoose,Es6 Promise,阅读我看到的文件 Mongoose异步操作,如.save()和查询,返回ES6承诺 我认为我熟悉ES6承诺,所以我写了如下内容: newUser.save().then(user => res.json({ id: user.id, name: user.name, email: user.email }) )
newUser.save().then(user =>
res.json({
id: user.id,
name: user.name,
email: user.email
})
)
.catch(
(err = res.status(500).json({
server: 'Something went wrong, please try again soon'
}))
);
我测试并发现一个成功的附加到DB中,但是无论如何都会调用一个500。记录的错误如下所示:
(node:40071) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
此外,使用堆栈跟踪:
(node:40071) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:40071) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
我在挠头,并与规格咨询
我很困惑为什么会出现错误,因为我认为我很清楚地处理了错误。而且,当我一起删除catch语句时,所有错误似乎都消失了!比如发生了什么事?我觉得这一行很可疑:
.catch((err=res.status(500).json({
如果你没有理解这意味着什么,err=>res.status…
请看=>
相比=/code>哇:|是的,很好地解决了我所有的问题。