简体   繁体   English

JavaScript承诺mongoose和bluebird缺少捕获和失败

[英]JavaScript Promises mongoose and bluebird missing catch and fail

I've started using promises, I use Node.js Mango (with mongoose) and bluebird.. The issue I'm having is for some reason when I chain the mongoose call with functions returning promises (I'm assuming this is the correct way to return and chain) then I get: 我已经开始使用promises,我使用Node.js Mango(带有mongoose)和bluebird ..我遇到的问题是由于某种原因,当我用返回promises的函数链接mongoose调用时(我假设这是正确的)返回和链的方式)然后我得到:

typeError: Object #<Promise> has no method 'fail'

if I change the fail to catch then I get the same issue: 如果我改变了捕获的失败,那么我得到同样的问题:

typeError: Object #<Promise> has no method 'catch'

what I do is use the function(null, function) pattern which is exactly fail and catch. 我所做的是使用函数(null,function)模式,它完全失败并捕获。 However the catch / fail is more readable. 然而,捕获/失败更具可读性。 Any clue why I'm getting this and how I should resolve this issue? 任何线索为什么我得到这个以及我应该如何解决这个问题?

Here is an example of the code block. 这是代码块的示例。

User.findOne({ 'email' :  user_email }).exec()
 }).then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping)
   .then (function (feUser) {
       return new Promise(function (resolve, reject) {
          res.json(feUser);
          return resolve(feUser);
      });
   }).fail/catch  (function (err) {
      console.log(err);
      sendError(res,"failed to get user",err);
   });

And here is the stacktrace: 这是堆栈跟踪:

TypeError: Object #<Promise> has no method 'catch'
    at module.exports.app.put.User.update.email (app\controllers\router.js:165:16)
    at callbacks (node_modules\express\lib\router\index.js:164:37)
    at isLoggedIn (app\controllers\router.js:741:10)
    at callbacks (node_modules\express\lib\router\index.js:164:37)
    at param (node_modules\express\lib\router\index.js:138:11)
    at param (node_modules\express\lib\router\index.js:135:11)
    at pass (node_modules\express\lib\router\index.js:145:5)
    at Router._dispatch (node_modules\express\lib\router\index.js:173:5)
    at Object.router (node_modules\express\lib\router\index.js:33:10)
    at next (node_modules\express\node_modules\connect\lib\proto.js:193:15)

mongoose 4.1+ maintainer suggestion: mongoose 4.1+维护者建议:

es2015 (es6): es2015(es6):

require('mongoose').Promise = Promise;

bluebird: 蓝鸟:

require('mongoose').Promise = require('bluebird');

Q: 问:

require('mongoose').Promise = require('q').Promise;

I do not know moongose, but in general, functions like fail or catch are convenience shortcuts and are not a part of the promises spec. 我不知道moongose,但一般来说,失败或捕获等功能都是方便快捷方式,不属于承诺规范的一部分。 As such the library does not need to have them to be promises-compliant. 因此,库不需要使它们符合承诺。 Apparently in your case they are not there. 显然在你的情况下他们不在那里。

You can achieve same functionality with then(successHandler, rejectionHandler) . 您可以使用then(successHandler, rejectionHandler)实现相同的功能。

In order to handle the promise rejection, you can rewrite your code as follows: 为了处理承诺拒绝,您可以按如下方式重写代码:

User.findOne({ 'email' :  user_email }).exec()
 }).then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping)
   .then (function (feUser) {
       return new Promise(function (resolve, reject) {
          res.json(feUser);
          return resolve(feUser);
      });
   }).then (undefined, function (err) {
      console.log(err);
      sendError(res,"failed to get user",err);
   });

Another way to do it is shown in the bluebird docs: 另一种方法是在蓝鸟文档中显示:

https://github.com/petkaantonov/bluebird/blob/master/API.md#promiseresolvedynamic-value---promise https://github.com/petkaantonov/bluebird/blob/master/API.md#promiseresolvedynamic-value---promise

You can wrap the mongoose promise in bluebird's Promise.resolve(), and you will get back a bluebird promise. 你可以在蓝鸟的Promise.resolve()中包含mongoose的承诺,你将获得蓝鸟的承诺。

 Promise.resolve(User.findOne({ 'email' :  user_email }).exec())
 .then (promisedTransformUserSchemaToFrontendObjectWithProjectMapping)
   .then (function (feUser) {
          res.json(feUser);
          return feUser;
   }).fail/catch  (function (err) {
      console.log(err);
      sendError(res,"failed to get user",err);
   });

It seems that the problem was mixing the two types of promises (bluebird and mongoose).. 似乎问题是混合了两种类型的承诺(蓝鸟和猫鼬)。

Once I used the promsifyAll on the db object, the catch started to work.. 一旦我在db对象上使用promsifyAll,catch就开始工作了..

 // promisify all model using mongoomise.. require('../../mongoomise').promisifyAll(mongoose, require('bluebird')) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM