简体   繁体   中英

Issue in deployment heroku - MongooseError: Operation `users.findOne()` buffering timed out after 10000ms

I have deployed my MERN chat application in heroku, it's working fine locally without any issues but having problem in connection to databse in heroku

I have checked heroku log

My Index.js file

enter code here 
2021-05-06T14:11:10.625862+00:00 app[web.1]: memory, and will not scale past a single process.
2021-05-06T14:11:10.640277+00:00 app[web.1]: SERVER STARTED! 10334
2021-05-06T14:11:10.738745+00:00 heroku[web.1]: State changed from starting to up
2021-05-06T14:11:40.686273+00:00 app[web.1]: MongooseServerSelectionError: Could not connect to 
any servers in your MongoDB Atlas cluster. One common reason is that you're trying to access the 
database from an IP that isn't whitelisted. Make sure your current IP address is on your Atlas 
cluster's IP whitelist: https://docs.atlas.mongodb.com/security-whitelist/
2021-05-06T14:11:40.686297+00:00 app[web.1]: at NativeConnection.Connection.openUri 
(/app/node_modules/mongoose/lib/connection.js:846:32)
2021-05-06T14:11:40.686298+00:00 app[web.1]: at /app/node_modules/mongoose/lib/index.js:350:10
2021-05-06T14:11:40.686298+00:00 app[web.1]: at 
/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5
2021-05-06T14:11:40.686299+00:00 app[web.1]: at new Promise (<anonymous>)
2021-05-06T14:11:40.686299+00:00 app[web.1]: at promiseOrCallback 
(/app/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)
2021-05-06T14:11:40.686300+00:00 app[web.1]: at Mongoose._promiseOrCallback   
(/app/node_modules/mongoose/lib/index.js:1154:10)
2021-05-06T14:11:40.686300+00:00 app[web.1]: at Mongoose.connect 
(/app/node_modules/mongoose/lib/index.js:349:20)  
2021-05-06T14:11:40.686301+00:00 app[web.1]: at file:///app/index.js:93:3
2021-05-06T14:11:40.686301+00:00 app[web.1]: at ModuleJob.run 
(internal/modules/esm/module_job.js:152:23)
2021-05-06T14:11:40.686302+00:00 app[web.1]: at async Loader.import 
(internal/modules/esm/loader.js:166:24)
2021-05-06T14:11:40.686302+00:00 app[web.1]: at async Object.loadESM 
(internal/process/esm_loader.js:68:5) {
2021-05-06T14:11:40.686302+00:00 app[web.1]: reason: TopologyDescription {
2021-05-06T14:11:40.686303+00:00 app[web.1]: type: 'ReplicaSetNoPrimary',
2021-05-06T14:11:40.686303+00:00 app[web.1]: setName: null,
2021-05-06T14:11:40.686304+00:00 app[web.1]: maxSetVersion: null,
2021-05-06T14:11:40.686304+00:00 app[web.1]: maxElectionId: null,
2021-05-06T14:11:40.686304+00:00 app[web.1]: servers: Map(3) {
2021-05-06T14:11:40.686305+00:00 app[web.1]: 'cluster0-shard-00-01.etje3.mongodb.net:27017' => 
[ServerDescription],
2021-05-06T14:11:40.686306+00:00 app[web.1]: 'cluster0-shard-00-02.etje3.mongodb.net:27017' => 
[ServerDescription],
2021-05-06T14:11:40.686306+00:00 app[web.1]: 'cluster0-shard-00-00.etje3.mongodb.net:27017' => 
[ServerDescription]
2021-05-06T14:11:40.686307+00:00 app[web.1]: },
2021-05-06T14:11:40.686307+00:00 app[web.1]: stale: false,
2021-05-06T14:11:40.686307+00:00 app[web.1]: compatible: true,
2021-05-06T14:11:40.686308+00:00 app[web.1]: compatibilityError: null,
2021-05-06T14:11:40.686308+00:00 app[web.1]: logicalSessionTimeoutMinutes: null,
2021-05-06T14:11:40.686308+00:00 app[web.1]: heartbeatFrequencyMS: 10000,
2021-05-06T14:11:40.686309+00:00 app[web.1]: localThresholdMS: 15,
2021-05-06T14:11:40.686309+00:00 app[web.1]: commonWireVersion: null
2021-05-06T14:11:40.686309+00:00 app[web.1]: }
2021-05-06T14:11:40.686310+00:00 app[web.1]: }


2021-05-06T14:12:40.521735+00:00 app[web.1]: MongooseError: Operation `users.findOne()` 
buffering timed out after 10000ms
2021-05-06T14:12:40.521747+00:00 app[web.1]: at Timeout.<anonymous> 
(/app/node_modules/mongoose/lib/drivers/node-mongodb-native/collection.js:185:20)
2021-05-06T14:12:40.521747+00:00 app[web.1]: at listOnTimeout (internal/timers.js:554:17)
2021-05-06T14:12:40.521750+00:00 app[web.1]: at processTimers (internal/timers.js:497:7)
2021-05-06T14:12:40.521786+00:00 heroku[router]: at=info method=POST path="/login" 
host=mychatapplicationmern.herokuapp.com request_id=595a5c0d-f22e-4a36-afdf-11b9579eaba1 
fwd="103.204.171.34" dyno=web.1 connect=0ms service=10012ms status=500 bytes=458 protocol=https

Two error which I am getting

  1. MongooseError: Operation users.findOne() buffering timed out after 10000ms
  2. MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster

For the second one, I have added my IP address in MongoDB atlas and even given access from every anywhere, but still the issue isn't resolved.

I have checked every answer in stackoverflow for first one, but none of the fix is working

Yesterday I had the same issue.

My case is that my app worked well a month ago and throw this error when I tried to open it yesterday.

If you are using atlas free tier Products, just go to their site and activate the cluster. It takes longer than they claim (3-5 minutes), in my case it took more than 10 minutes. After the waiting period, you refresh the app and it may work. This may not be your case but hope this helps a little bit.

I also checked these two posts, although they did not help me, it seems pretty relevant. https://dev.to/arunkc/solve-mongooseerror-operation-x-find-buffering-timed-out-after-10000ms-3d3j

https://dev.to/veroca88/set-up-mongodb-atlas-and-connect-with-node-js-avoid-mongooseerror-operation-x-findone-buffering-timed-out-after-10000ms-5dpi

In my code the connection part is still in this way:

mongoose.connect(process.env.MONGODB_URI || 
'mongodb://localhost/posts');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
  console.log('DB connected!');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM