S
S
Slaxik2020-07-07 15:27:36
JavaScript
Slaxik, 2020-07-07 15:27:36

Losing connection with mongdb?

connection to the database is lost and an error appears

UnhandledPromiseRejectionWarning: MongoTimeoutError: Server selection timed out after 30000 ms
    at Timeout.setTimeout [as _onTimeout] (/root/logistician_deploy_07.07.2020/node_modules/mongodb/lib/core/sdam/server_selection.js:309:9)
    at ontimeout (timers.js:482:11)
    at tryOnTimeout (timers.js:317:5)
    at Timer.listOnTimeout (timers.js:277:5)
(node:26567) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:26567) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.


THE CODE:
router.get('/distance', async (req,res) =>{
  var url = require('url');
  var url_parts = url.parse(req.url, true);
  var query = url_parts.query;

  const distances = await loadAllDistances();
      try{
          let dis = await distances.find({'fromCity':query.fromCity,'toCity':query.toCity}).toArray();
          if(query.fromCity === query.toCity){
            return res.send({'fromCity':query.fromCity,'toCity':query.toCity,'distation':0});
          }else if(dis.length === 0){
            return res.send('NOT')
          }else{
              return res.send(dis);
          }

      }catch(err){
        return res.send(err)
      }
  });
async function loadAllDistances(){
  const client = await mongodb.MongoClient.connect(
      'mongodb://localhost:27017/logistician',
  {
      useNewUrlParser: true,
      useUnifiedTopology: true 
  });
  return client.db('logistician').collection('distance');
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alex_Coder, 2020-07-07
@Alex_Coder

Does the connection appear at all?
And how long does it take for it to disappear?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question