Reputation: 11
I am facing this TypeError: "Cannot read property 'replace' of undefined" when I made a small application with nodejs
, express.js
, MongoDB
, mongoose
and when I connect my express applications with mongoose at that time when I replace my password I face this type of error
const mongoose = require('mongoose');
const dotenv = require('dotenv');
const app = require('./app');
dotenv.config({ path: './config.env' });
const DB = process.env.DATABASE.replace(
'<PASSWORD>',
process.env.DATABASE_PASSWORD
);
mongoose
.connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useFindAndModify: false,
})
.then((con) => {
console.log(con.connections);
console.log('DB connection succesful!!');
});
const port = process.env.PORT || 3001;
app.listen(port, (req, res) => {
console.log(`App is running on ${port}...`);
});
How to resolve this error?
Upvotes: 1
Views: 2304
Reputation: 1
I also have the same issue and I solved it run the whole command in one line, like this:
node .\development\data\import-data.js
Upvotes: 0
Reputation: 45
The issue comes from your path that point to file config.env. You should keep config.env the same folder with the file that you want to execute. An example is below:
Hope that solves your issue.
Upvotes: 0
Reputation: 11
config.env
file, to check thatconsole.log(process.env);
// use thie before const DB to check file is accessible
If you access to .env
now check DATABASE
and DATABASE_PASSWORD
is mentioned correctly.
If everything is written correct then there is problem with your password to correct your password by customise it and don't use special character.
2.1 to reset password of mongoDB Atlas cluster got to DATABASE ACCESS click on it now it will show your user, at right end click to edit.
2.2 now popup open, edit your password and save it. and got config.env
file change it.
IN config.env
file contain and check these should be written correctly. DATABASE url you get from atlas just copy and paste it just replace password to PASSWORD. thats it.
NODE_ENV=development
PORT=8000
DATABASE=mongodb+srv://***USER_name***:<PASSWORD>@cluster0.awemngr.mongodb.net/***DATABASENAME***?retryWrites=true&w=majority
DATABASE_PASSWORD= your_password
Upvotes: 0
Reputation: 81
The issue usually occurs due to not assigning the environment configuration file path properly. Pay attention to the dotenv.config({ path: './config.env' });
line. You are searching for the config.env file within the current folder. Make sure that the file exists on the current working directory. If not, assign the path of the config file properly. The issue will be solved right after that.
Upvotes: 1
Reputation: 11
I got the same error and i resolved by correcting my config.env file where
DATABASE = mongodb+srv://shailesh:@cluster0.n1pjr.mongodb.net/natours?retryWrites=true/majority?
I removed characters after true/ i.e. Majority.
However if this does not solve your problem then just ensure that Database is registered as an environment variable. Because error pertains to the issue that application is not able to find environment variable called "DATABASE"
Upvotes: 0