Mongoose right promise rejection processing

I'm struggling a bit with the promise template in nodeJS

I search for the user in db and then save a new object with a link to the user, but when the user is not in db, I have to return the rejection, but I'm not sure how to do it correctly.

is there a way how to make it more beautiful?

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()
)
+4
source share
1 answer

You can use throwinside thencallbacks to reject them. Or instead of using a constructor Promiselike this, you can 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()
+3
source

All Articles