【问题标题】:Getting -MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"- when deploying with Railway得到 -MongooseError:`openUri()` 的`uri` 参数必须是一个字符串,得到 \"undefined\"- 使用 Railway 部署时
【发布时间】:2022-11-16 22:44:58
【问题描述】:

我知道还有其他类似的问题,但对我没有任何帮助。

当我在本地托管时,它可以工作,但我收到 Railway 的错误消息。完整的错误信息是:MongooseError: The 'uri' parameter to 'openUri() must be a string, got "undefined". Make sure the first parameter to 'mongoose.connect()' or 'mongoose.createConnection()' is a string.

我的 URI 字符串在 .env 文件中为 MONGO_URI="mongodb+srv://<user>:<password>@cluster0.jju88.mongodb.net/<collection>?retryWrites=true&w=majority".env 文件位于 config 文件夹中,但它说在其他地方将其放在根目录中。所以我改变了它,但它没有帮助。

我在顶部的 server.js 文件中尝试了 require("dotenv").config({ path: ".env" })require("dotenv").config()。在我把.env改成root之前,是require("dotenv").config({ path: "./config/.env" })

数据库配置如下所示:

    const connectDB = async () => {
      try {
        const conn = await mongoose.connect(process.env.MONGO_URI, {
          useNewUrlParser: true,
          useCreateIndex: true,
          useUnifiedTopology: true,
          useFindAndModify: false,
        });
    
        console.log(`MongoDB Connected: ${conn.connection.host}`);
      } catch (err) {
        console.error(err);
        process.exit(1);
      }
    };

我有 dotenv 作为依赖项安装。

我错过了什么?

【问题讨论】:

    标签: mongodb mongoose


    【解决方案1】:

    您需要将 MONGO_URI 设置为 Railway 上的变量。

    变量名=MONGO_URI
    值 = mongodb+srv://USERNAME:PASSWORD@CLUSTERNAME.san3z5m.mongodb.net/DBNAME?retryWrites=true&w=majority

    【讨论】:

      猜你喜欢
      • 2023-02-11
      • 2021-05-22
      • 2021-03-24
      • 2019-10-05
      • 2023-03-31
      • 2019-08-11
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      相关资源
      最近更新 更多