【问题标题】:bluebird Promisies crud example using nodejs , express and mongoose使用 nodejs 、 express 和 mongoose 的 bluebird Promisies crud 示例
【发布时间】:2014-10-22 16:33:37
【问题描述】:

我的朋友们,很遗憾,我找不到任何关于如何在 node js express mongoose 应用程序中实现 bluebird promise 库的示例。

我的应用程序设置在 mongoose 模型、控制器和路由位于不同文件中的位置。

但是用猫鼬实现它,我就是想不通。

所以请有人告诉我它是如何使用的。请看下文。

//express controller Article.js


var mongoose = require('mongoose'),
errorHandler = require('./errors'),
Article = mongoose.model('Article');

exports.list = function(req, res) {
Article.find().sort('-created').populate('user', 'displayName').exec(function(err, articles) {
      if (err) {
          return res.status(400).send({
            message: errorHandler.getErrorMessage(err)
          });
      } else {
          res.jsonp(articles);
      }
  });
};

//猫鼬模型

 /**
 * Module dependencies.
 */
 var mongoose = require('mongoose'),
 Schema = mongoose.Schema;

 /**
 * Article Schema
 */
 var ArticleSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    title: {
        type: String,
        default: '',
        trim: true,
        required: 'Title cannot be blank'
    },
    content: {
        type: String,
        default: '',
        trim: true
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

mongoose.model('Article', ArticleSchema);

如果我想使用 Bluebird Promise 库,请问如何更改 export.list

提前致谢。

一些问题,

在猫鼬模型上我在哪里调用 promisify? 例如Article = mongoose.model('Article'); like thisArticle = Promise.promisifyAll(require('Article')); 要么 像这样

  var Article = mongoose.model('Article');
  Article = Promise.promisifyAll(Article);

【问题讨论】:

    标签: javascript node.js express mongoose bluebird


    【解决方案1】:

    好吧,在网上搜索了几周后,我找到了一个例子here 为了在您的 nodejs 应用程序中使用猫鼬模型,

    您需要像这样承诺库和模型实例 在你的模型模块中,在你定义了你的模式之后

    var Article = mongoose.model('Article', ArticleSchema);
    Promise.promisifyAll(Article);
    Promise.promisifyAll(Article.prototype);
    
    exports.Article = Article;
    //Replace Article with the name of your Model.
    

    现在你可以像这样在你的控制器中使用你的猫鼬模型作为一个承诺

    exports.create = function(req, res) {
        var article = new Article(req.body);
        article.user = req.user;
    
        article.saveAsync().then(function(){
            res.jsonp(article);
        }).catch(function (e){
            return res.status(400).send({
                message: errorHandler.getErrorMessage(e)
            });
        });
      };
    

    【讨论】:

      【解决方案2】:

      你实际上可以在你的模型顶部做一个更简单的单行。

      var mongoose = require('bluebird').promisifyAll(require('mongoose'));
      

      像平常一样创建模型,瞧。一切都为你承诺。

      【讨论】:

      • 你知道如何用 es6 做到这一点吗?我将如何承诺import mongoose from 'mongoose';
      • 我相信它会类似于:import { mongoose } from 'mongoose'; import { bluebird } from 'bluebird'; var mongoose = bluebird.promisifyAll(mongoose); 但我不确定。无论哪种方式,您都可以将 commonJS 与 es6 混合,所以我不会担心@JesusAdolfoRodriguez
      【解决方案3】:

      即使是棘手的问题也已经得到解答。我认为承诺猫鼬的更好方法是执行以下操作:

      Promise.promisifyAll(mongoose.Model);
      Promise.promisifyAll(mongoose.Model.prototype);
      Promise.promisifyAll(mongoose.Query.prototype);
      

      这样所有模块都会自动拥有异步功能。

      另请参阅:mongoose-bird 了解正在执行此操作的库。

      【讨论】:

        猜你喜欢
        • 2022-09-23
        • 1970-01-01
        • 1970-01-01
        • 2014-09-07
        • 1970-01-01
        • 2014-03-16
        • 2020-11-12
        • 2014-08-20
        • 2011-06-28
        相关资源
        最近更新 更多