【问题标题】:Connecting NodeJS to Atlas MongoDB将 NodeJS 连接到 Atlas MongoDB
【发布时间】:2019-08-24 02:26:02
【问题描述】:

我编写了一些脚本,以便使用 NodeJS 和 MongoDB 创建服务器和数据库。

我已经使用 localhost 尝试了代码,一切正常。

我在 Atlas MongoDB 创建了一个帐户,以便在在线数据库上测试应用程序。

我更改了我的 js 中的 url,因为它只在连接字符串时由 Atlas 提供:

const url = "mongodb+srv://XXX:<XXX>@XXX-uyrjt.mongodb.net/test?retryWrites=true&w=majority";

MongoClient.connect(url,{useNewUrlParser: true}, function(err,client) {

    if(err)
        console.log('unable to connect to mongodb server.error',err);
    else{...

}

但是我得到以下错误:

【问题讨论】:

标签: node.js mongodb mongodb-atlas


【解决方案1】:

请在这里仔细检查两件事

  1. 正确的用户名和密码
  2. 您的 ip 在 Atlas 帐户中被列入白名单。要禁用白名单,您可以使用 ip - 0.0.0.0/0

【讨论】:

  • 已检查,均正常。我在某处读到可能是因为使用了 useNewUrlParser: true 但只在一个地方找到它
猜你喜欢
  • 2021-06-25
  • 2019-03-14
  • 2020-09-13
  • 2018-12-07
  • 2021-09-20
  • 2018-11-11
  • 2019-08-19
  • 2019-10-01
  • 1970-01-01
相关资源
最近更新 更多