0

I am currently unsuccessfully trying to persist document to mongodb using nodejs and mongoosejs.

This my code structure:

code Structure

The start.js simply initializes my app and creates mongod connection detailed in mongodb.js. The connection is succesfully established and can be seen in mongodb console.

this my 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 is my 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 I call the save method in datalayer.js the "inside save >>>>> " statement logged to console and then nothing happens. It seems the var regModel = new model.RegistrationModel(data); statement fails without any error and I donot have any clue why. Any suggestion would be highly appreciated.

sample post data as requested :

{
    "userId":"tesrwetId",
    "name":"Test",
    "mob":"2536245632",
    "email":"ashdjas@sa.com"
}

Update 24/06

I am starting to think that since the connection to mongoose is established after my model initialization this problem is experienced. I am unable to get the connection to happen before model is initialized.

Below is the file initialisation sequence. Though the mongodb.js(the mongoose connection) is initilaized first, the on connect method gets called after model.js is initialized.

enter image description here

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 to ' + configure.creds.mongodb);
  require('./model');
});

// If the connection throws an error
mongoose.connection.on('error',function (err) {
  console.log('MOngodb connection error: ' + err);
});

// When the connection is 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 this understanding regarding loading sequence is incorrect. read here

Rohit
  • 2,132
  • 1
  • 15
  • 24
  • Can you specify what `data` is when this fails? – dylants Jun 22 '14 at 15:43
  • @dylants added sample post data. – Rohit Jun 22 '14 at 19:30
  • I have tried executing your code, same folder structure, copy pasted code from this page, as you have mentioned, its working fine for me. I have got console - inside mongoose save. Could you please mail me(email-from myprofile) your sample code. – Harpreet Singh Jun 23 '14 at 02:15
  • @HarpreetSingh please see updated question. Your email is not part of your profile. – Rohit Jun 23 '14 at 19:03

1 Answers1

0

Might have something to do with you're connection using the mongodb library and then trying to save using mongoose. Mongoose has mongodb as a dependency and is probably not resolving to the connection you've made. Remove your mongodb code and use the mongoose connect() function to establish db connection.

something like:

mongoose.connect('mongodb://localhost:port/test', function(err){ if(err) console.log(err); else console.log('Connected'); });

Yousef
  • 401
  • 2
  • 8
  • You can also verify if connections exist by console.log(mongoose.connections.length). I believe not be an empty array. – Yousef Jun 23 '14 at 05:28
  • Thanks for the suggestion Yousef. The mongodb.js uses mongoose to connect mongodb, the name of the file is misleading. – Rohit Jun 23 '14 at 18:43