MongoError: getaddrinfo ENOTFOUND undefined undefined:27017

巧了我就是萌 提交于 2019-12-07 04:01:17

问题


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:

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?


回答1:


I think you are not providing the PORT NO. required for mongoDB.

Please give the port no.(27017) along with localhost.

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.

I hope this helps.




回答2:


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.




回答3:


The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI (MONGOLAB_URI may be a legacy thing).




回答4:


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!

Thanks for everyone who was helping out!




回答5:


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.




回答6:


The Heroku environment variable that gets created for an mLab add-on is called MONGODB_URI (MONGOLAB_URI may be a legacy thing). (pneumee)

This solved the problem for me.



来源:https://stackoverflow.com/questions/39108992/mongoerror-getaddrinfo-enotfound-undefined-undefined27017

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!