My model is
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),
}
And I use this in 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});
});
}
};
When I call save controller work fine but when I call get it throw exception in console same line where db.User.find is being called, I also log find method in console and this also print undefined. I am using mongoose version 4.0.1.
Aucun commentaire:
Enregistrer un commentaire