[英]error while connecting to mongoDB in Node.js, 'first parameter to mongoose.connect() is a string.'
I got error as below while running app.js运行 app.js 时出现如下错误
message: 'The uri
parameter to openUri()
must be a string, got "undefined". message: ' openUri()
的uri
参数必须是一个字符串,得到“未定义”。 Make sure the first parameter to mongoose.connect()
or mongoose.createConnection()
is a string.',确保mongoose.connect()
或mongoose.createConnection()
的第一个参数是一个字符串。',
name: 'MongooseError'名称:'猫鼬错误'
app.js file is as follow- app.js 文件如下-
const express = require('express');
const path = require('path')
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const employeeRoutes = require('./routes/employee');
const app = express();
dotenv.config({path:'./config.env'}); //Setting configuration file
app.set('views',path.join(__dirname,'views')); //setting views folder
app.set('view engine','ejs'); //setting view engine as ejs
app.use(express.static('public')); //setting static folder
mongoose.connect(process.env.DATABSE_LOCAL.toString(),{
useNewUrlParser : true,
useUnifiedTopology : true,
useCreateIndex : true
});
const port = process.env.PORT;
app.listen(port,(port)=>{
console.info("server started at 127.0.0.1:"+port);
});
this is my config.env这是我的 config.env
PORT = 3000
DATABASE_LOCAL = mongodb://localhost:27017/employees
I see what you are doing ;我明白你在做什么; however you don't need to worry about using toString() method Simply leave it like this :但是您无需担心使用 toString() 方法只需像这样保留它:
mongoose.connect(process.env.DATABASE_LOCAL)
The only reason it didn't work for you was because you had a small typo它对你不起作用的唯一原因是因为你有一个小错字
mongoose.connect(process.env.DATABSE_LOCAL)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.