尝试连接到mongolab

尝试连接到mongolab

本文介绍了尝试连接到mongolab MongoDB数据库时2行NodeJS应用程序在mongoose.connect()上崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下NodeJS代码:

I have the following NodeJS code:

var mongoose = require('mongoose');
mongoose.connect('mongodb://dev:[email protected]:31632/mongodev');

然后使用node server.js运行它时,它将挂断几秒钟并抛出以下内容:

And upon running it with node server.js, it hangs up for a few seconds and throws the following:

C:\Users\dev\work\code\local\nodejsplayground\restwithmongo\nod
dules\mongoose\node_modules\mongodb\lib\server.js:228
        process.nextTick(function() { throw err; })
                                            ^
Error
    at Object.<anonymous> (C:\Users\dev\work\code\local\nodejsp
round\restwithmongo\node_modules\mongoose\node_modules\mongodb\node_modules\mong
core\lib\error.js:42:24)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (C:\Users\dev\work\code\local\nodejsp
round\restwithmongo\node_modules\mongoose\node_modules\mongodb\node_modules\mong
core\index.js:2:17)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)

我通过控制台通过控制台ping服务器:

I pinged the server by console via this:

ping ds031632.mongolab.com

我尝试使用Windows Installer安装mongodb,但仍无法正常工作

I tried installing mongodb with the windows installer and it's still not working

推荐答案

当连接到mongodb时发生错误而没有调用错误回调时,将发生此错误.要解决此错误(并获取实际错误),请向.connect方法添加回调,或者绑定到error事件.

This error happens when there is an error connecting to mongodb without an error callback to be called. To fix this error (and get the actual error,) add a callback to the .connect method, or, bind to the error event.

mongoose.connect(config.mongodb, function (err) {
  if (err) {
    console.log(err);
  }
});

mongoose.connect(config.mongodb);

var db = mongoose.connection;

db.on('error', function (err) {
  console.log('mongodb connection error: %s', err);
  process.exit();
});
db.once('open', function () {
  console.log('Successfully connected to mongodb');
  app.emit('dbopen');
});


如果您发现什么都没有发生并且只是挂起,请等待30秒钟左右,然后它将超时,这仅表示猫鼬无法连接到mongodb,这可能是由很多不同的事物引起的,这些事物大多是相关的到网络/dns/防火墙/服务器配置.


If you find that nothing happens and it just hangs, wait 30 or so seconds and it will timeout, which simply means mongoose couldn't connect to mongodb, which could be caused by a very large number of different things, mostly related to network/dns/firewall/server configuration.

这篇关于尝试连接到mongolab MongoDB数据库时2行NodeJS应用程序在mongoose.connect()上崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 21:41