我在nodeJS中与promise模式斗争
I'm bit fighting with promise pattern in nodeJS
我在db中寻找用户然后用用户引用保存新实体,但是当用户不是在db中,我应该返回拒绝,但我不确定如何正确地做到这一点。
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.
有没有办法如何做得更好?
is there way how to do it more nicely?
btw:抱歉,coffeescript: - [
btw: sorry, 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() )推荐答案
你可以在然后回调中使用 throw 来拒绝它们。或者,您也可以使用 Promise 构造函数。 / JavaScript / Reference / Global_Objects / Promise / rejectrel =nofollow> Promise.reject(404) 。
You can use throw inside then callbacks to reject them. Or, instead of using the Promise constructor like that, you might also use Promise.reject(404).
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()更多推荐
Mongoose正确承诺拒绝处理
发布评论