Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bluebird Promisies crud example using nodejs , express and mongoose

My Fellow Friends, Unfortunately I cant find any examples on how to implement the bluebird promise library in a node js express mongoose app.

My app is setup where the mongoose model, controllers and routes are in diffrent files.

But implementing it with mongoose, i just cant figure it out.

So Please can someone show me how its used. Please see below.

//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);
      }
  });
};

//Mongoose Model

 /**
 * 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);

So please if i wanted to use Bluebird promise library, How would i go about changing export.list

Thanks in advance.

Some Questions,

where do i call promisify on the mongoose model? e.g Article = mongoose.model('Article'); like thisArticle = Promise.promisifyAll(require('Article')); or like this

  var Article = mongoose.model('Article');
  Article = Promise.promisifyAll(Article);
like image 463
mojotaker Avatar asked Aug 28 '14 17:08

mojotaker


2 Answers

Ok after scouring around the internet for weeks, i was able to find an example here In order to work with mongoose models in your nodejs app,

You need to promisify the Library and the model Instance like so in your model module, after you have defined your Schema

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

exports.Article = Article;
//Replace Article with the name of your Model.

Now you can use your mongoose model as a promise in your controller like this

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)
        });
    });
  };
like image 179
mojotaker Avatar answered Sep 29 '22 13:09

mojotaker


You actually can do an even simpler one-liner at the top of your Model.

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

Create your model like normal, and voila. Everything is promisified for you.

like image 28
Mark Pieszak - Trilon.io Avatar answered Sep 29 '22 12:09

Mark Pieszak - Trilon.io