尝试连接到MongoDB失败并发出一条非常无用的错误消息:
trying to connect to Mongo...
trying to set up collection...
/home/mike/dev/bitbucket/mqtt-mongo/node_modules/mongodb/lib/mongodb/mongo_client.js:409
throw err
^
[object Object]
它会在trying to connect to Mongo...
挂起很长时间,然后在失败时吐出trying to set up collection...
,并且永远不会在collection set up
函数的末尾达到setupCollection()
。
我的app.js:
var mqtt=require('mqtt');
var mongodb=require('mongodb');
var mongodbClient=mongodb.MongoClient;
//var mongodbURI='mongodb://user:pass@host:27017/nodejs';
var mongodbURI='mongodb://host:27017/nodejs';
var deviceRoot="owntracks/mike";
var collection,client;
console.log("trying to connect to Mongo...")
mongodbClient.connect(mongodbURI,setupCollection);
function setupCollection(err,db) {
console.log("trying to set up collection...")
if(err) throw err;
collection=db.collection("owntracks");
client=mqtt.createClient(mqtt_port,'mqtt_host')
client.subscribe(deviceRoot+"#")
client.on('message', insertEvent);
console.log("collection set up")
}
function insertEvent(topic,payload) {
正如您所看到的,我尝试使用和不使用相同行为的用户名和密码。我还没有尝试使用当地的Mongo实例。
mongo_client.js
中第409行的周围函数是:
399 // Set up the db options
400 var db = new Db(object.dbName, serverConfig, object.db_options);
401 // Open the db
402 db.open(function(err, db){
403 if(err) {
404 return process.nextTick(function() {
405 try {
406 callback(err, null);
407 } catch (err) {
408 if(db) db.close();
409 throw err
410 }
411 });
412 }
我的问题是: