尝试将节点js与mongoDB Atlas云连接时收到UnhandledPromiseRejectionWarning错误

时间:2019-11-13 06:45:27

标签: node.js mongodb

我创建了一个app.js文件,在那里我试图与mongoDB地图集连接。错误'UnhandledPromiseRejectionWarning:未处理的承诺被拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是因为我在终端中运行时拒绝了未使用.catch()处理的诺言而产生的。

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 个答案:

答案 0 :(得分:3)

尝试将异步函数操作放在try catch块中,如下所示。我希望这可以完成工作。

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

答案 1 :(得分:1)

尝试一下:

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

答案 2 :(得分:0)

尝试这种方法:

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

答案 3 :(得分:0)

尝试将函数的所有内容包装在try / catch块中:

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