繁体   English   中英

无法从本地节点应用程序连接到 MongoDB Atlas

[英]Unable to Connect with MongoDB Atlas from Local Node App

我在 atlas 上创建了一个集群,并尝试使用我的节点应用程序进行连接并使用 mongoose 记录连接状态。 我已将我的 ip 列入白名单并正确设置所有内容,但我不断收到UnhandledPromiseRejectionWarning

这是我的db.js代码。 错误抛出mongooose.connect(url, opts)

 const mongoose = require('mongoose');

 const db_connect = async () => {
 const conn_string = await mongoose.connect('mongodb+srv://devjoe: 
    <password_hidden_delibarately>@devcamper-gs1nb.mongodb.net/devcamper?retry 
    Writes=true&w=majority', 
   {
        useCreateIndex: true,
        useNewUrlParser: true,
        useFindAndModify: false,
        useUnifiedTopology: true
   }); 

   console.log(`connection string: ${conn_string.connection.host}`);

}

module.exports = db_connect;

server.js文件中,我只是调用了db_connect();类的函数db_connect(); 使用 commonjs 模块导入后。

任何帮助将不胜感激,因为我找不到问题所在。 谢谢。

如果解决方案不起作用,您也可以尝试此操作:

const mongoose = require("mongoose");

const db_connect = () => {
  try {
    const conn_string = mongoose.connect(
      "mongodb+srv://devjoe: <*****************>@devcamper-gs1nb.mongodb.net/devcamper?retry Writes=true&w=majority",
      {
        useCreateIndex: true,
        useNewUrlParser: true,
        useFindAndModify: false,
        useUnifiedTopology: true
      }
    );

    console.log(`connection string: ${conn_string.connection.host}`);
  } catch {
    console.log(`not connected to : ${conn_string.connection.host}`);
  }
};

module.exports = db_connect;

我刚刚在我的电脑上测试了这个解决方案,它有效!

但是,如果这些都不起作用,可以向您发送我如何进行连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM