Javascript 使用promise模块连接mongodb时出错
我是新来的Javascript 使用promise模块连接mongodb时出错,javascript,node.js,mongodb,node-modules,Javascript,Node.js,Mongodb,Node Modules,我是新来的nodejs。我正在使用连接的promise模块 mongodb。但我面临以下错误 (node:6827) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'db' of undefined at /home/luan1412167/Desktop/nodejs_tutorial/mongodb/promise.js:11:21 at processTicksAndRejections
nodejs
。我正在使用连接的promise
模块
mongodb
。但我面临以下错误
(node:6827) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'db' of undefined
at /home/luan1412167/Desktop/nodejs_tutorial/mongodb/promise.js:11:21
at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:6827) 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(). (rejection id: 1)
我努力想把它修好。但我还没修好。
任何建议都将不胜感激。非常感谢。因为您使用了承诺,所以需要使用catch语句修改代码。按如下方式修改您的代码:
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true})
.then((db) => {
var db = client.db('Employee');
}).catch((err) => {
console.error('An error occurred connecting to MongoDB: ', err);
});
谢谢你的回答。然而,我认为我在上面的代码中使用了它作为一个实例。我的错误在连接函数处。嗯,也许我不明白你的想法。所以你可以跟我详细谈谈。
MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true})
.then((db) => {
var db = client.db('Employee');
}).catch((err) => {
console.error('An error occurred connecting to MongoDB: ', err);
});