[英]I got error while connecting to mongodb in nodejs
I have folder structure as我的文件夹结构为
.env .env
MONGODB_URL = mongodb://localhost/EmployeeDB
.config .config
export default {
MONGODB_URL: process.env.MONGODB_URL
}
.server.js .server.js
import express from 'express'
import mongoose from 'mongoose'
import config from './config'
import dotenv from 'dotenv'
const app = express()
const PORT = 5000
dotenv.config()
const MONGODB_URL = config.MONGODB_URL
mongoose.connect(MONGODB_URL,{
useCreateIndex: true,
useNewUrlParser: true,
useUnifiedTopology: true
}, error=>{
if(error){
console.log("Error occurred "+error)
}else{
console.log("Database successfully connected")
}
})
app.listen(PORT,()=>{
console.log("Server started at port "+PORT)
})
Now it throws error as Error [MongooseError]: The uri
parameter to openUri()
must be a string, got "undefined".现在它抛出错误为 Error [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.确保mongoose.connect()
或mongoose.createConnection()
的第一个参数是一个字符串。
If I put string directly in.config file as如果我将字符串直接放在.config文件中
export default {
MONGODB_URL: "mongodb://localhost/EmployeeDB"
}
It works perfectly but, if I use process.env.MONGODB_URL
then it throws the error which I have mentioned above.它工作得很好,但是如果我使用process.env.MONGODB_URL
那么它会抛出我上面提到的错误。 Why it is throwing error when I use process.env.MONGODB_URL
?为什么当我使用process.env.MONGODB_URL
时会抛出错误?
I think you have to assign variables in .env
without whitespaces around assignment sign =
我认为您必须在.env
中分配变量,而分配符号周围没有空格=
MONGODB_URL=mongodb://localhost/EmployeeDB
dotenv
will load the variables in .env
file and assign it to process.env
when you call dotenv.config()
, which happens after when you imported ./config
.当您调用dotenv.config()
时, dotenv
将加载.env
文件中的变量并将其分配给process.env
,这发生在您导入./config
之后。
But you can not call dotenv.config()
before an import
statement, so dotenv
provides you an option to use import 'dotenv/config'
, so you have to make sure that line comes before import config from './config'
, best way is to put it at the top.但是您不能在import
语句之前调用dotenv.config()
,因此dotenv
为您提供了使用import 'dotenv/config'
的选项,因此您必须确保该行出现在import config from './config'
之前,最好方法是把它放在顶部。
import dotenv from 'dotenv/config'
import express from 'express'
import mongoose from 'mongoose'
import config from './config'
const MONGODB_URL = config.MONGODB_URL
/* ... */
You are trying to use the dotenv on the .config
file, but the dontenv is not available on it.您正在尝试在.config
文件上使用 dotenv,但 dontenv 在其上不可用。
A good approach is create a config
directory with a index.js file inside of it, there you can share the environment variables and use the dotenv module in your code.一个好的方法是创建一个包含 index.js 文件的config
目录,在那里您可以共享环境变量并在代码中使用 dotenv 模块。
Your code will be something like this:您的代码将是这样的:
config/index.js :配置/index.js :
require('dotenv').config();
module.exports = {
MONGODB_URL: process.env.MONGODB_URL,
};
.env : .env :
MONGODB_URL=mongodb://localhost/EmployeeDB
And then you be able to use the env variable anywhere you want simply calling:然后你可以在任何你想调用的地方使用 env 变量:
server.js : server.js :
const config = require('../config');
console.log(`Mongo URL: ${config.MONGODB_URL}`)
In this way your code get a little more organized "IMHO".通过这种方式,您的代码会变得更有条理“恕我直言”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.