node.js - Unable to persist data using nodejs mongoosejs to mongodb -
node.js - Unable to persist data using nodejs mongoosejs to mongodb -
i unsuccessfully trying persist document mongodb using nodejs , mongoosejs.
this code structure:
the start.js initializes app , creates mongod connection detailed in mongodb.js. connection succesfully established , can seen in mongodb console.
this model.js
var mongoose = require('mongoose'); var regsceham = new mongoose.schema({ userid: string, name:string, mob:string, email:string }) var registrationmodel = mongoose.model('registration', regsceham); exports.registrationmodel = registrationmodel;
this datalayer.js:
var mongoose = require('mongoose'); var mongodb = require('./mongodb'); var model = require('./model'); //var registrationmodel = mongoose.model('registrationmodel'); ///////////////////////////////// function save(data){ console.log('inside save >>>>> ' + data); var regmodel = new model.registrationmodel(data); console.log('after model'); regmodel.save(function(err,regmodel){ console.log('inside mongoose save'); if(err) console.log(err); }); } require('./model') exports.save = save;
when phone call save method in datalayer.js "inside save >>>>> " statement logged console , nil happens. seems var regmodel = new model.registrationmodel(data);
statement fails without error , donot have clue why. suggestion highly appreciated.
sample post info requested :
{ "userid":"tesrwetid", "name":"test", "mob":"2536245632", "email":"ashdjas@sa.com" }
update 24/06 i starting think since connection mongoose established after model initialization problem experienced. unable connection happen before model initialized.
below file initialisation sequence. though mongodb.js(the mongoose connection) initilaized first, on connect method gets called after model.js initialized.
the mongodb.js file:
console.log('mongodb....'); var mongoose = require('mongoose'); var configure = require('./configure'); // create database //console.log('sadasdasdsa >>>>>>>' + configure.creds.mongodb) mongoose.connect(configure.creds.mongodb); mongoose.connection.on('connected', function () { console.log('mongodb connection open ' + configure.creds.mongodb); require('./model'); }); // if connection throws error mongoose.connection.on('error',function (err) { console.log('mongodb connection error: ' + err); }); // when connection disconnected mongoose.connection.on('disconnected', function () { console.log('mongodb connection disconnected'); }); process.on('sigint', function() { mongoose.connection.close(function () { console.log('mongodb connection disconnected through app termination'); process.exit(0); }); }); require('./model');
update 28/06 i have since realised understanding regarding loading sequence incorrect. read here
might have you're connection using mongodb library , trying save using mongoose. mongoose has mongodb dependency , not resolving connection you've made. remove mongodb code , utilize mongoose connect() function found db connection.
something like:
mongoose.connect('mongodb://localhost:port/test', function(err){ if(err) console.log(err); else console.log('connected'); });
node.js mongodb mongoose restify
Comments
Post a Comment