【问题标题】:MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined"MongooseError:`openUri()` 的 `uri` 参数必须是字符串,得到“未定义”
【发布时间】:2021-03-24 02:12:45
【问题描述】:

我正在使用 dotenv 将我的数据库名称和密码存储在我的 backend 文件夹的根目录中的 process.env 文件中,该文件被 .gitignore 忽略。我在backend/config 下有一个db.js 文件,它正在尝试使用process.env

require('dotenv').config()
const mongoose = require("mongoose");

mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true })
  .then(() => {
    console.log('MongoDB Connected…')
  })
  .catch(err => console.log(err))

require("../models/Task");

但我收到以下错误

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.

我的process.env是这样写的

MONGO_URI="mongodb+srv://<dbname>:<dbpw>@cluster0.f9k2w.mongodb.net/quotesDB?retryWrites=true&w=majority"

&lt;dbname&gt;&lt;dbpw&gt; 包含我的秘密数据库名称和密码。

为什么我的process.env 变量未定义?

我的github

【问题讨论】:

  • 你提到了process.env文件,如果这是你的文件名,请将它重命名为.env,如果当前文件是.env你可以尝试重新安装dotenv希望这会解决你的问题

标签: node.js mongodb mongoose


【解决方案1】:

将您的 process.env 文件重命名为 .env

这是一个.env 文件,而不是process.env 文件。其他一切看起来都很好。

【讨论】:

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