我的模特是
user.js的
module.exports = function (mongoose) {
var schema = new mongoose.Schema({
name : String,
emailAddress : String,
lastActivity: { type: Date, default: Date.now },
isActive : Boolean
});
return mongoose.model('User', schema);
}
db helper class db.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/MyDatabase');
var db = mongoose.connection;
console.log('Try to connect to MongoDB via Mongoose ...');
db.on('error', console.error.bind(console, 'Mongoose connection error:'));
db.once('open', function callback() {
console.log('Connected to MongoDB !');
});
module.exports = {
User: require('../models/user.js')(mongoose),
}
我在sails js controller中使用它
var db = require('../services/db.js');
module.exports = {
save : function(req,res){
var user = db.User({
name: "govind",
emailAddress: "govind@govind.com",
isActive : true
});
user.save(function(){
res.json({"response" : "successfully saved sample data"});
});
},
get : function(req,res){
console.log(db.User.find); //this print undefined
db.User.find(function (err, s) {
res.json({"response" : s});
});
}
};
当我调用save controller工作正常但是当我调用get它在调用db.User.find的控制台同一行中抛出异常时,我也在控制台中记录find方法,这也打印undefined。我正在使用mongoose版本4.0.1。