使用MongoSkin重新连接MongoDB(Nodejs)

时间:2012-07-06 09:46:43

标签: node.js mongodb mongoskin

我一直在使用MongoSkin(Nodejs)插件来连接Mongodb,如下所示。

    var db = mongo.db(dbconfig.mongo_ip+'/'+dbconfig.mongo_db_name);
    db.collection('myprofile').findOne({_id:memberid}, function(err, session){

        if(err){
            db.close();
            console.log(err.stack);
        }

    })

如果我从node.js得到mongo连接错误,那么如何使用mongoskin重新连接相同的错误,

Mongo Connect errr: Error: failed to connect to [localhost:27017]
    at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/server.js:184:51)
    at [object Object].emit (events.js:45:17)
    at [object Object].<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:110:14)
    at [object Object].emit (events.js:45:17)
    at Socket.<anonymous> (/usr/local/lib/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/connection.js:301:10)
    at Socket.emit (events.js:42:17)
    at Array.33 (net.js:799:27)
    at EventEmitter._tickCallback (node.js:108:26)

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

一个好的起点是

https://github.com/kissjs/node-mongoskin/blob/master/test/db.js

但我认为你需要在mongoskin和驱动程序下调用db。