More precisely, which URI do I use in the mongoose connect method when deploying my app to Heroku and how do I connect it to MongoDB Atlas?
Currently my code looks like this and it works well on my local server:
mongoose.connect('mongodb://localhost:27017/facebook')
After deploying on Heroku and on trying to access the app, I get an application error. I ran the heroku logs --tail command and I get the following error:
2022-02-16T04:22:16.055858 00:00 app[web.1]: _hostAddress: HostAddress { isIPv6: false, host: 'localhost', port: 27017 },
2022-02-16T04:22:16.055858 00:00 app[web.1]: address: 'localhost:27017',
2022-02-16T04:22:16.055858 00:00 app[web.1]: type: 'Unknown',
2022-02-16T04:22:16.055859 00:00 app[web.1]: hosts: [],
2022-02-16T04:22:16.055859 00:00 app[web.1]: passives: [],
2022-02-16T04:22:16.055860 00:00 app[web.1]: arbiters: [],
2022-02-16T04:22:16.055860 00:00 app[web.1]: tags: {},
2022-02-16T04:22:16.055860 00:00 app[web.1]: minWireVersion: 0,
2022-02-16T04:22:16.055861 00:00 app[web.1]: maxWireVersion: 0,
2022-02-16T04:22:16.055861 00:00 app[web.1]: roundTripTime: -1,
2022-02-16T04:22:16.055861 00:00 app[web.1]: lastUpdateTime: 570484147,
2022-02-16T04:22:16.055862 00:00 app[web.1]: lastWriteDate: 0,
2022-02-16T04:22:16.055862 00:00 app[web.1]: error: MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017
2022-02-16T04:22:16.055862 00:00 app[web.1]: at connectionFailureError (/app/node_modules/mongodb/lib/cmap/connect.js:381:20)
2022-02-16T04:22:16.055863 00:00 app[web.1]: at Socket.<anonymous> (/app/node_modules/mongodb/lib/cmap/connect.js:301:22)
2022-02-16T04:22:16.055863 00:00 app[web.1]: at Object.onceWrapper (node:events:640:26)
2022-02-16T04:22:16.055863 00:00 app[web.1]: at Socket.emit (node:events:520:28)
2022-02-16T04:22:16.055864 00:00 app[web.1]: at emitErrorNT (node:internal/streams/destroy:157:8)
2022-02-16T04:22:16.055864 00:00 app[web.1]: at emitErrorCloseNT (node:internal/streams/destroy:122:3)
2022-02-16T04:22:16.055864 00:00 app[web.1]: at processTicksAndRejections (node:internal/process/task_queues:83:21)
2022-02-16T04:22:16.055864 00:00 app[web.1]: }
2022-02-16T04:22:16.055865 00:00 app[web.1]: },
2022-02-16T04:22:16.055865 00:00 app[web.1]: stale: false,
2022-02-16T04:22:16.055865 00:00 app[web.1]: compatible: true,
2022-02-16T04:22:16.055865 00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2022-02-16T04:22:16.055865 00:00 app[web.1]: localThresholdMS: 15,
2022-02-16T04:22:16.055866 00:00 app[web.1]: logicalSessionTimeoutMinutes: undefined
2022-02-16T04:22:16.055866 00:00 app[web.1]: }
What can I do to fix this?
CodePudding user response:
This URL 'mongodb://localhost:27017/facebook' should only be used when you have a MongoDB server running on the same server as the app, as you can see there localhost is in the URL string, it throws an error because it can't find any running MongoDB database on the server which your app is running on Heroku
And to connect it with MongoDB atlas is quite easy, just go ahead create an account and a cluster mongodb doc, in there you will find your cluster URl. Replace this current URL with it and you should be good.
And Yes I skipped some part about whitelisting IP addresses and user permission, but MongoDB atlas should prompt you to do so
CodePudding user response:
Follow this article step by step. https://dev.to/dalalrohit/how-to-connect-to-mongodb-atlas-using-node-js-k9i