【发布时间】:2019-11-05 14:51:28
【问题描述】:
不知从何而来,我得到了这个错误。
{ MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:189:13)
at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:321:5)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at TLSSocket.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
at Object.onceWrapper (events.js:277:13)
name: 'MongoNetworkError',
errorLabels: [ 'TransientTransactionError' ],
[Symbol(mongoErrorContextSymbol)]: {} }
(node:1854) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [cluster0-shard-00-01-erhon.mongodb.net:27017] on first connect [MongoNetworkError: connection 5 to cluster0-shard-00-01-erhon.mongodb.net:27017 closed]
at Pool.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/topologies/server.js:431:11)
at Pool.emit (events.js:189:13)
at connect (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/pool.js:557:14)
at callback (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:109:5)
at runCommand (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:129:7)
at Connection.errorHandler (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connect.js:321:5)
at Object.onceWrapper (events.js:277:13)
at Connection.emit (events.js:189:13)
at TLSSocket.<anonymous> (/Users/theodosiostziomakas/nodejs-tutorials/09_Working with MongoDB/07_Finishing the Update Product code/node_modules/mongodb-core/lib/connection/connection.js:350:12)
at Object.onceWrapper (events.js:277:13)
(node:1854) 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:1854) [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
我正在使用 Mongo Atlas(意味着我安装了 mongodb),这是我连接 mongodb 的方式。
const mongoConnect = callback => {
MongoClient
.connect('mongodb+srv://theodosiostziomakas:password_goes_here@cluster0-erhon.mongodb.net/shop?retryWrites=true&w=majority',
{useNewUrlParser: true}
)
.then(client => {
console.log('Connected!');
_db = client.db();
callback();
})
.catch(err => {
console.log(err);
throw err;
});
}
你也可以从这个repo下载我的项目。
如何解决此错误?我也查看了这个topic,但找不到解决方案。
谢谢, 西奥。
【问题讨论】:
标签: mongodb