Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用promise模块连接mongodb时出错_Javascript_Node.js_Mongodb_Node Modules - Fatal编程技术网

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