小编say*_*gas的帖子

如何在 Node js 和 Express 应用程序中使用 .env 和 ES6 模块?

我需要您帮助了解如何在此应用程序上使用 .env 文件。这是我的问题:我正在我的 Node Express 应用程序中使用 ES6 模块构建一个应用程序。我在将变量存储在 .env 文件中时遇到问题,下面这两种方法都会给出此错误: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. did not connect。但是当我只使用纯字符串连接时,这意味着我没有正确使用 dotenv 文件:

1-

import {} from "dotenv/config.js";
import express from "express";
import mongoose from "mongoose";
import cors from "cors";

const app=express()
...
//DB config
mongoose.connect(process.env.CONNECTION_URL,
    {
      useCreateIndex: true,
      useNewUrlParser: true,
      useUnifiedTopology: true,
    })

app.listen(port,()=>console.log(`server on ${port}`) 
Run Code Online (Sandbox Code Playgroud)

2-

import dotenv from "dotenv";
import …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js express

5
推荐指数
1
解决办法
9469
查看次数

标签 统计

express ×1

mongodb ×1

node.js ×1