Javascript 使用nodejs、express和mongoose的bluebird Promises crud示例
我的朋友们,不幸的是,我找不到任何关于如何在node js express mongoose应用程序中实现bluebird promise库的示例 我的应用程序是安装在猫鼬模型,控制器和路线在不同的文件 但是用猫鼬实现它,我就是想不出来 所以请有人告诉我它是怎么用的。请看下面Javascript 使用nodejs、express和mongoose的bluebird Promises crud示例,javascript,node.js,express,mongoose,bluebird,Javascript,Node.js,Express,Mongoose,Bluebird,我的朋友们,不幸的是,我找不到任何关于如何在node js express mongoose应用程序中实现bluebird promise库的示例 我的应用程序是安装在猫鼬模型,控制器和路线在不同的文件 但是用猫鼬实现它,我就是想不出来 所以请有人告诉我它是怎么用的。请看下面 //express controller Article.js var mongoose = require('mongoose'), errorHandler = require('./errors'), Artic
//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在哪里?
e、 gArticle=mongoose.model('Article');
像这样
Article=Promise.promisifyAll(require('Article'));
或
像这样
var Article = mongoose.model('Article');
Article = Promise.promisifyAll(Article);
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)
});
});
};
在网上搜索了几个星期后,我找到了一个例子 为了在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.
现在,您可以像这样在控制器中使用mongoose模型作为承诺
var Article = mongoose.model('Article');
Article = Promise.promisifyAll(Article);
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)
});
});
};
即使是棘手的问题也已经得到了回答。我认为向猫鼬示好的一个更好的方法是:
Promise.promisifyAll(mongoose.Model);
Promise.promisifyAll(mongoose.Model.prototype);
Promise.promisifyAll(mongoose.Query.prototype);
这样,所有模块都自动具有异步功能
另请参见:有关完全执行此操作的库。实际上,您可以在模型顶部执行更简单的一行程序
var mongoose = require('bluebird').promisifyAll(require('mongoose'));
像普通一样创建你的模型,瞧。一切都是为你准备的 您知道如何使用es6实现这一点吗?我将如何承诺从“猫鼬”导入猫鼬代码>我相信它会是这样的:
从“猫鼬”导入{mongoose};从“蓝鸟”中导入{bluebird};变种猫鼬=蓝知更鸟promisifyAll(猫鼬)代码>但我不确定。您可以将commonJS与es6混合使用,这样我就不用担心@JesusAdolfoRodriguez