mongodb MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39108992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
提问by txnnxr
So I'm working my way though the Getting Mean book from Manning and following the steps in Chapter 5 I'm trying to use a db on Mongolab as an add-on to Heroku. When I run this code (both locally and on Heroku) it returns this error:
因此,我正在按照 Manning 的 Getting Mean 一书按照自己的方式工作,并按照第 5 章中的步骤操作,我尝试将 Mongolab 上的数据库用作 Heroku 的附加组件。当我运行此代码(在本地和 Heroku 上)时,它返回此错误:
MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
MongoError: getaddrinfo ENOTFOUND undefined undefined:27017
This is my current code:
这是我当前的代码:
var mongoose = require('mongoose');
var dbURI = "mongodb://localhost/loc8r";
if (process.env.NODE_ENV === 'production') {
dbURI = process.env.MONGOLAB_URI;
}
mongoose.connect(dbURI);
Troubleshooting I started the app from the terminal with:
故障排除我从终端启动了应用程序:
NODE_ENV=production MONGOLAB_URI=/*my mongo uri here*/
with that it runs fine locally. So I'm not sure where the issue is coming from. Any suggestions for troubleshooting the error listed?
它在本地运行良好。所以我不确定问题来自哪里。对列出的错误进行故障排除的任何建议?
采纳答案by txnnxr
So I'm not sure what part of this process fixed the issue but I completely removed/deleted the mongolab addon from Heroku. Then I added it back on and performed the same exact steps with the same code and it worked!
所以我不确定这个过程的哪一部分解决了这个问题,但我从 Heroku 中完全删除/删除了 mongolab 插件。然后我重新添加它并使用相同的代码执行相同的确切步骤并且它起作用了!
Thanks for everyone who was helping out!
感谢所有提供帮助的人!
回答by Ravi Shankar Bharti
I think you are not providing the PORT NO.
required for mongoDB
.
我想,你是不是在提供PORT NO.
必需的mongoDB
。
Please give the port no.(27017)
along with localhost.
请port no.(27017)
与本地主机一起提供。
Try this:
尝试这个:
var dbURI = "mongodb://127.0.0.1:27017/loc8r";
getaddrinfo ENOTFOUND
means client was not able to connect to the given address. Please try with the above address.
getaddrinfo ENOTFOUND
表示客户端无法连接到给定地址。请尝试使用上述地址。
I hope this helps.
我希望这有帮助。
回答by Itguymax
You just missed specifying the port number as shown:
您只是错过了指定端口号,如下所示:
var dbURI = "mongodb://localhost:27017/thenDBname"
Make sure to change localhost while hosting on production server.
确保在生产服务器上托管时更改 localhost。
回答by pneumee
The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI
(MONGOLAB_URI
may be a legacy thing).
为 mLab 附加组件创建的 Heroku 环境变量被调用MONGODB_URI
(MONGOLAB_URI
可能是遗留的东西)。
回答by Max Tamtam
The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI (MONGOLAB_URI may be a legacy thing).(pneumee)
为 mLab 附加组件创建的 Heroku 环境变量称为 MONGODB_URI(MONGOLAB_URI 可能是遗留的东西)。(呼吸)
This solved the problem for me.
这为我解决了这个问题。
回答by Adam
This looks correct. I've seen this error (a lot) before as well. The 'undefined undefined' I believe is referring to your environment variables being undefined. Try console logging your environment variables to make sure they're valid.
这看起来是正确的。我以前也见过这个错误(很多)。我认为“未定义未定义”是指您的环境变量未定义。尝试控制台记录您的环境变量以确保它们有效。