【问题标题】:I am getting UnhandledPromiseRejectionWarning error when trying to connect node js with mongoDB Atlas cloud尝试将节点 js 与 mongoDB Atlas 云连接时出现 UnhandledPromiseRejectionWarning 错误
【发布时间】:2020-03-08 22:16:58
【问题描述】:

我创建了一个 app.js 文件,并尝试连接 mongoDB atlas。错误'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)
})

【问题讨论】:

  • 向我们展示您的代码
  • 添加了我的代码@DaniyalLukmanov
  • 提供错误详情,可能错误不是来自connect函数。

标签: node.js mongodb


【解决方案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)
})

【讨论】:

  • 我尝试了你的代码,但仍然遇到同样的错误。
  • 你还收到UnhandledPromiseRejectionWarning error 吗?我只是专注于修复该错误。实际上,您的原始代码本身存在一些故障。
  • 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( ); }); } 捕捉 (e) { 拒绝(e); } }) }
  • @Steer 供参考,docs.mongodb.com/ecosystem/drivers/node 让我编辑我的答案。
【解决方案2】:

尝试将异步函数操作放在 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)
})

【讨论】:

    【解决方案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 });
    
         // 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)
    })
    

    【讨论】:

      【解决方案4】:

      试试这个方法:

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

      【讨论】:

      • 我尝试了上述所有解决方案,但仍然遇到同样的错误。
      猜你喜欢
      • 2020-10-26
      • 2021-09-07
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2020-08-14
      • 1970-01-01
      • 2020-02-26
      • 2020-11-27
      相关资源
      最近更新 更多