Bluebird A simple crud example using nodejs, express and mongoose

My friends, friends, unfortunately, I can not find examples of how to implement the bluebird promise library in the node js express mongoose application.

My application is set up where the mongoose model, controllers and routes are in different files.

But realizing it with the help of mongoose, I just can not understand it.

So, please, someone will show me how it is used. 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 the Bluebird promise library, how would I go about changing export.list

Thanks in advance.

Some questions,

where can i call the promisify mongoose model? e.g. Article = mongoose.model('Article'); like this Article = mongoose.model('Article'); like this Article = Promise.promisifyAll (require ('Article')); or how is it

  var Article = mongoose.model('Article'); Article = Promise.promisifyAll(Article); 
+6
source share
3 answers

Well, having worked for several weeks on the Internet, I managed to find an example here. To work with Mongolian models in your nodejs application,

You need to promise the library and model, for example, in your model module, after you define 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) }); }); }; 
+10
source

In fact, you can make an even simpler single line layer at the top of your model.

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

Build your model, as usual, and voila. All is promised to you.

+2
source

Even the tough question has already been answered. I think the best way to promise a mongoose is to do the following:

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

Thus, all modules automatically have Async features.

See also: mongoose-bird for a library that does just that.

+1
source

Source: https://habr.com/ru/post/974508/


All Articles