I am using the MERN stack for my web app. My database is deployed to MongoDB Atlas. My code was working perfectly fine, but after I moved all the passwords and port numbers to a .env file. I got the below errors:
Here is my code:
const express = require("express");
const mongoose = require('mongoose');
const dotenv = require('dotenv').config();
const app = express();
const port = process.env.PORT;
const mydbURL = process.env.DB_URL;
mongoose.connect(mydbURL, {useNewUrlParser: true, useUnifiedTopology: true, useFindAndModify: true});
in the .env file:
PORT = 5000
DB_URL = mongodb+srv://username:password@name-of-cluster.aezwt.mongodb.net/name-of-db?retryWrites=true&w=majority
I am getting the following error:
(node:12580) UnhandledPromiseRejectionWarning: MongooseError: The
uri
parameter toopenUri()
must be a string, got "undefined". Make sure the first parameter tomongoose.connect()
ormongoose.createConnection()
is a string.
(node:12580) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag
--unhandled-rejections=strict
(see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode ). (rejection id: 1) (node:12580) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
My Question:
What might be a possible fix to the above error ?
试试这对我有用
dotenv.config({ path: path.join(__dirname, 'your env file path') });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.