简体   繁体   中英

I am getting UnhandledPromiseRejectionWarning error when trying to connect node js with mongoDB Atlas cloud

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)
})

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)
})

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)
})

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();
});

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)
})

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