简体   繁体   English

在 Node.js 中连接到 mongoDB 时出错,“mongoose.connect() 的第一个参数是一个字符串。”

[英]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.

相关问题 `openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。 确保 `mongoose.connect()` 或 `mongoose 的第一个参数 - The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose MongooseError:`openUri()` 的 `uri` 参数必须是字符串,得到“未定义”。 `mongoose.connect()`ion()` 的第一个参数是一个字符串 - MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". first parameter to `mongoose.connect()`ion()` is a string 将 mongodb 连接到 node.js 时出错 - getting error while connecting mongodb to node.js mongoDB与node.js连接时出现问题 - issue while connecting mongoDB with node.js Mongoose.connect 不抛出任何错误,当 Mongodb 未运行时 - Mongoose.connect not throwing any error, when Mongodb is not running 使用Node.js在MongoLab中连接到mongoDB时出错 - Getting error while connect to mongoDB in MongoLab using Node.js mongoose.connect(),第一个参数应该是字符串,接收未定义 - mongoose.connect(), first argument should be String, received undefined MongoDB,Node.Js(mongoose)中的okForStorage错误 - not okForStorage Error in MongoDB,Node.Js(mongoose) Node.js猫鼬连接错误 - Node.js mongoose connect error 无法连接 MongoDB Atlas 与 Node.js 槽 Mongoose - Not able to connect MongoDB Atlas with Node.js trough Mongoose
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM