[英]Mongoose right promise rejection handling
I'm bit fighting with promise pattern in nodeJS 我在nodeJS中与promise模式斗争
I'm looking for user in db and then saving new entity with user reference, but when user is not in db, I should return rejection, but I'm not sure how to do it properly. 我正在寻找db中的用户,然后使用用户引用保存新实体,但是当用户不在db中时,我应该返回拒绝,但我不确定如何正确执行。
is there way how to do it more nicely? 有没有办法如何更好地做到这一点?
btw: sorry, coffeescript :-[ 顺便说一句:抱歉,coffeescript: - [
User.findOne({'fbId':userData.me.id}).exec().then((doc)->
if !doc? then return new Promise (resolve,reject)->reject(404)
video = new Video({
user:doc
state: "queue"
createdAt: new Date()
})
video.save().exec()
)
You can use throw
inside then
callbacks to reject them. 你可以使用
throw
inside then
回调来拒绝它们。 Or, instead of using the Promise
constructor like that, you might also use Promise.reject(404)
. 或者,您也可以使用
Promise.reject(404)
,而不是像这样使用Promise
构造函数。
User.findOne
fbId:userData.me.id
.exec().then (doc)->
if !doc?
throw new Error 404
video = new Video
user: doc
state: "queue"
createdAt: new Date
video.save().exec()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.