How to correct the “connection.readyState” of mongoose in mocha from “connecting” to “connected?

200 views
Skip to first unread message

justin

unread,
Apr 23, 2013, 1:56:32 AM4/23/13
to mongoo...@googlegroups.com

I have created a model of users(registration) in mongoose. This model is accessed from test case created in mocha as well as register form from front end. The new user can be saved from register form through front end, but the test case written in mocha can't be saved.

While I check through the code the connection.readyState of mongoose is (connecting) on test case and (connected) for user register form.

models/users.js

var mongoose = require('mongoose');

mongoose
.connect('mongodb://localhost/test');

console
.log('conn ready:  '+mongoose.connection.readyState);

// "conn ready: 2"  i.e connecting for test case as well as from user register form


var Schema = mongoose.Schema,
   
ObjectId = Schema.ObjectId,

   
UserSchema = new Schema({

       
// schemas

   
});


UserSchema.statics.newUser = function (uname, email, pass) {

   
var instance = new User();

    instance
.uname  = uname;

    instance
.email  = email;

    instance
.pass   = pass;

    console
.log("conn state: "+mongoose.connection.readyState);

   
// "conn state: 2"  i.e connecting for test case. But  1  i.e connected for  user register form

    instance
.save(function (err) {
     
// Do rest
   
});

};

var User = mongoose.model('User', UserSchema);
exports
.User = User;

How to correct the connection.readyState of mongoose on test cases in mocha?
Reply all
Reply to author
Forward
0 new messages