2
votes

I created a app.js file and there I am trying to connect with mongoDB atlas. The error '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()' is throwing when I run in terminal.

const connect = async function () {
  const MongoClient = require('mongodb').MongoClient;
  const uri = "mymongoDB atals url for nodejs";
  MongoClient.connect(uri, { useNewUrlParser: true });

  const collection = client.db("feedback").collection("itinerary");
  // perform actions on the collection object
  client.close();

};
connect().then(() => {
  console.log('handle success here');
}).catch((exception) => {
  console.log('handle error here: ', exception)
})
4
Show us your codeDaniyal Lukmanov
Added my code @DaniyalLukmanovSteer
Provide the error detail, maybe the error is not come from connect function.hoangdv

4 Answers

3
votes

Try putting the async function operations in try catch block as below. I hope this should do the work.

const connect = async function () {
  try {
    const MongoClient = require('mongodb').MongoClient;
    const uri = "mymongoDB atals url for nodejs";
    MongoClient.connect(uri, { useNewUrlParser: true });

    const collection = client.db("feedback").collection("itinerary");
    // perform actions on the collection object
    client.close();
  } catch (e) {
    console.log("Error", e)
  }

};

connect().then(() => {
  console.log('handle success here');
}).catch((exception) => {
  console.log('handle error here: ', exception)
})
2
votes

Try this:

const MongoClient = require('mongodb').MongoClient;

const connect = function () {
  return new Promise((resolve, reject) => {
    try {
      const uri = "mymongoDB atals url for nodejs";
      const client = new MongoClient(uri, { useNewUrlParser: true });
      client.connect(err => {
        if (err) {
          reject(err)
        }
        const collection = client.db("feedback").collection("itinerary");
        client.close();
        resolve();
      });
    } catch (e) {
      reject(e);
    }
  }) 
};
connect().then(() => {
  console.log('handle success here');
}).catch((exception) => {
  console.log('handle error here: ', exception)
})
0
votes

Try this approach:

const MongoClient = require('mongodb').MongoClient;

// replace the uri string with your connection string.
const uri = "mymongoDB atals url for nodejs"
MongoClient.connect(uri, function(err, client) {
   if(err) {
        console.log('handle error here: ');
   }
   console.log('handle success here');
   const collection = client.db("feedback").collection("itinerary");
   // perform actions on the collection object
   client.close();
});
0
votes

Try by wrapping all the content of your function in a try/catch block:

const connect = async function () {
  try {
     const MongoClient = require('mongodb').MongoClient;
     const uri = "mymongoDB atals url for nodejs";
     MongoClient.connect(uri, { useNewUrlParser: true });

     // most probably this is throwing the error. Notice the extra await
     const collection = await client.db("feedback").collection("itinerary");

     // perform actions on the collection object
     client.close();
  } catch (e) {
    console.log(`Caught error`,e)
  }

};
connect().then(() => {
  console.log('handle success here');
}).catch((exception) => {
  console.log('handle error here: ', exception)
})