node.js - Strange issue, model.find undefined in mongoose -
my model
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 connect mongodb via mongoose ...'); db.on('error', console.error.bind(console, 'mongoose connection error:')); db.once('open', function callback() { console.log('connected mongodb !'); }); module.exports = { user: require('../models/user.js')(mongoose), }
and use 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"}); }); }, : function(req,res){ console.log(db.user.find); //this print undefined db.user.find(function (err, s) { res.json({"response" : s}); }); } };
when call save controller work fine when call throw exception in console same line db.user.find being called, log find method in console , print undefined. using mongoose version 4.0.1.
Comments
Post a Comment