Mongoose正确承诺拒绝处理

编程入门 行业动态 更新时间:2024-10-22 23:42:28
本文介绍了Mongoose正确承诺拒绝处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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正确承诺拒绝处理

本文发布于:2023-11-26 00:04:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631869.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:正确   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!