我确实尝试在我的集合中创建一个静态方法,但不进行回调
我的代码是:
// db.js
var MySchema = new mongoose.Schema({
name: {type: String, required: true, unique: true},
userId: {type: mongoose.Schema.Types.ObjectId, ref: 'User', required: true },
});
MySchema.statics.findDashboardByUserID = function (userid, callback) {
console.log("id ===> "+userid);
this.find({ userId: mongoose.Types.ObjectId(userid) }, 'name', callback);
};
MySchema.statics.findByName = function (name, callback) {
console.log("name ===> "+name);
this.find({ name: name }, 'index', callback);
};
// Build the MySchema model
var MySchema = mongoose.model('MySchema', MySchema);
// app.js
var mydb = require('./db');
var mongoose = require('mongoose');
var MySchema = mongoose.model( 'MySchema' );
exports.getUserList = function (req, res) {
MySchema.findByUserID(req.params.userid, function (err, user) {
if(!err){
console.log(user);
res.json(user);
}else{
console.log(err);
res.json({"status":"error", "error":"Error"});
}
});
MySchema.findByName('foo', function (err, userName) {
if(!err){
console.log(userName);
res.json(userName);
}else{
console.log(err);
res.json({"status":"error", "error":"Error"});
}
});
};
//console
id ===> 77777777ffffff0000000011
name ===> First
我没有回电。我的app.js中没有console.log,但我的db.js中只有console.log