【发布时间】: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函数。