andy-zou
andy-zou

Reputation: 29

MongoError: server instance in invalid state undefined

This is a website based on nodejs and express.Database is Mongodb.
The problem is:
when I visit http://xxx/u/username, It's Ok.

But after I edit my page and redirect to http://xxx/u/username, It's wrong.The data in mongodb changed.

Here are error info:

MongoError: server instance in invalid state undefined at Function.MongoError.create (\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:29:11) at Server.connect (\node_modules\mongodb\node_modules\mongodb-core\lib\topologies\server.js:336:22) at Server.connect (\node_modules\mongodb\lib\server.js:355:17) at open (\node_modules\mongodb\lib\db.js:223:19) at Db.open (\node_modules\mongodb\lib\db.js:246:44) at Function.User.get (\models\user.js:45:11) at \routes\index.js:171:8 at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5) at next (\node_modules\express\lib\router\route.js:131:13) at Route.dispatch (\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (\node_modules\express\lib\router\layer.js:95:5) at \node_modules\express\lib\router\index.js:277:22 at param (\node_modules\express\lib\router\index.js:349:14) at param (\node_modules\express\lib\router\index.js:365:14) at Function.process_params (\node_modules\express\lib\router\index.js:410:3) at next (\node_modules\express\lib\router\index.js:271:10)

Upvotes: 2

Views: 2105

Answers (1)

Kirill Shur
Kirill Shur

Reputation: 290

Here is working snippet,I hope this will help

   var c = console;


    var app = require('express')();


    var http = require('http').createServer(app);




    var Db = require('mongodb').Db, 
    Server = require('mongodb').Server;
    var db = new Db('stock',new Server('localhost',27017));



    var io = require('socket.io')(http,{path:'/infocenter/sockets'});



    /****socket.io connection*****/
    io.on('connection',function(socket){

    console.log("Connected");

    /****registration details***/
    socket.on('regdetails',function(data){


    db.open(function(err,mdb){

     if(err){

        c.log(err);

     }  

    c.log("Mongo Registration Worked");

     /***insert user if not exists***/
     mdb.collection('users').find({name:data.fname}).toArray(function(err,results){

      c.log(results.length);    
      if(results.length ==  0)
      {
        mdb.collection('users').insert({name:data.fname,pass:data.upass});
        socket.emit('regmessage',{'message':'success','status':1}); 
        mdb.close();
      }
      else{
       socket.emit('regmessage',{'message':'user existed'}); 
       mdb.close();
      }

    });
    /***end insert user if not exists***/

    });


    });

    /****end registration details***/




    /***login details***/

    socket.on('logindetails',function(data){



    db.open(function(err,mdb){

     if(err){

        c.log(err);

     }  

    c.log("Login Mongo Worked");


    /***check if login is correct***/

    mdb.collection('users').find({name:data.fname,pass:data.upass}).toArray(function(err,results){


      if(results.length ==  1)
      {
        socket.emit('logmessage',{'message':'login accepted','status':1});
        mdb.close();

      }
      else{
         socket.emit('logmessage',{'message':'login is invalid','status':0});
         mdb.close();   
      } 

    });

    /***end check if login is correct***/


    });


    });

    /***end login details***/






    });


    /*****end socket.io connection *****/



    var port = 7000;


    http.listen(port,function(){

     console.log("Registration socket running on port:"+port);  

    });

Upvotes: 0

Related Questions