初始化 db.js
const mongoose = require('mongoose');
const Config = require('../../config');
mongoose.Promise = require('bluebird');
user.js
const User = require('./db/mongodb').UserModel;
exports.findById = function(username){
return User.findOne({username:username}).exec();
};
userService.js
exports.getUser = function(username,password){`
return User.findById(username).then((userInfo) => {
return Promise.resolve(userInfo);
}else{
return Promise.reject('No Such User');
}
这样明显比回调的写法简单清晰多了