使用猫鼬连接到MongoDb Atlas

时间:2018-09-11 11:16:51

标签: node.js mongodb mongoose

我正在尝试使用atlas提供的URL字符串连接到MongoDb Atlas:

mongoose
  .connect(
    "mongodb+srv://user:@contactlistcluster-hk1w5.mongodb.net/test?retryWrites=true"
  )
  .then(() => {
    console.log("Connected to database!");
  })
  .catch((error) => {
    console.log("Connection failed!");
    console.log(error);
  });

但是我在运行时遇到此错误:

**TypeError: Cannot read property 'close' of undefined
    at topology.connect (C:\dev\contact-api_bind\node_modules\mongoose\node_modules\mongodb\lib\operations\mongo_client_ops.js:412:16)
    at ReplSet.<anonymous> (C:\dev\contact-api_bind\node_modules\mongoose\node_modules\mongodb\lib\topologies\replset.js:364:11)
    at Object.onceWrapper (events.js:255:19)
    at ReplSet.emit (events.js:160:13)
    at C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\replset.js:631:23
    at Server.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\replset.js:357:9)
    at Object.onceWrapper (events.js:255:19)
    at Server.emit (events.js:160:13)
    at Pool.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\topologies\server.js:562:21)
    at Pool.emit (events.js:160:13)
    at Connection.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\connection\pool.js:316:12)
    at Object.onceWrapper (events.js:255:19)
    at Connection.emit (events.js:160:13)
    at TLSSocket.<anonymous> (C:\dev\contact-api_bind\node_modules\mongodb-core\lib\connection\connection.js:245:50)
    at Object.onceWrapper (events.js:255:19)
    at TLSSocket.emit (events.js:160:13)
[nodemon] app crashed - waiting for file changes before starting...**

1 个答案:

答案 0 :(得分:0)

问题已解决。实际上,我的组织正在使用代理服务器来阻止连接。

在我的个人计算机上工作得很好。