连接集群mongodb的错误

时间:2019-05-01 07:18:51

标签: node.js mongodb express

我正在使用nodejs连接到mongodb,但出现此错误 我添加了IP,但没有任何反应

(node:7412) UnhandledPromiseRejectionWarning: MongoNetworkError: connection 5 to cluster0-shard-00-02-xobf0.mongodb.net:27017 closed
    at TLSSocket.<anonymous> (C:\Users\user\Desktop\NodeJS - The Complete Guide\node_modules\mongodb-core\lib\connection\connection.js:352:9)
    at Object.onceWrapper (events.js:277:13)
    at TLSSocket.emit (events.js:189:13)
    at _handle.close (net.js:597:12)
    at TCP.done (_tls_wrap.js:388:7)
(node:7412) 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)
(node:7412) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[nodemon] clean exit - waiting for changes before restart

1 个答案:

答案 0 :(得分:0)

尝试一下,

 const MongoClient = require("mongodb").MongoClient;
 const uri ="mongodb+srv://user:userpassword@meanapp-srlw9.mongodb.net/test?retryWrites=true";
 const client = new MongoClient(uri, { useNewUrlParser: true });
 client.connect(err => {
 const collection = client.db("test").collection("devices");
 // perform actions on the collection object
   console.log("connencted");
 client.close();
});

现在,将您的群集设置更改为可以从任何地方连接