最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mongoose .env returning undefined - Stack Overflow

programmeradmin4浏览0评论

I am setting up my database connection using a MEVN stack but I am getting the following error;

The `uri` parameter to `openUri()` must be a string, got "undefined"

If I try console log process.env.DATABASE_URL it just returns undefined. What have I done wrong here's my code;

index.js

import dotenv from 'dotenv';
import Express from 'express';
import Mongoose from 'mongoose';

dotenv.config();

const app = Express();

Mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });

app.listen(3000, () => {
    // console.log(process.env.DATABASE_URL);
    console.log('server started on port 3000');
});

.env

DATABASE_URL="mongodb+srv://reece:<password>@mevn-tutorial-cluster-egjs6.mongodb/auth?retryWrites=true&w=majority"

I removed my password for obvious reasons

I am setting up my database connection using a MEVN stack but I am getting the following error;

The `uri` parameter to `openUri()` must be a string, got "undefined"

If I try console log process.env.DATABASE_URL it just returns undefined. What have I done wrong here's my code;

index.js

import dotenv from 'dotenv';
import Express from 'express';
import Mongoose from 'mongoose';

dotenv.config();

const app = Express();

Mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true });

app.listen(3000, () => {
    // console.log(process.env.DATABASE_URL);
    console.log('server started on port 3000');
});

.env

DATABASE_URL="mongodb+srv://reece:<password>@mevn-tutorial-cluster-egjs6.mongodb/auth?retryWrites=true&w=majority"

I removed my password for obvious reasons

Share Improve this question asked Mar 30, 2020 at 15:09 ReeceReece 2,71111 gold badges52 silver badges104 bronze badges 2
  • What do you get when you do console.log(process.env.DATABASE_URL);? – Ankit Agarwal Commented Mar 30, 2020 at 15:13
  • @AnkitAgarwal undefined – Reece Commented Mar 30, 2020 at 15:32
Add a ment  | 

4 Answers 4

Reset to default 4

You have to create a .env file in the root dir of your application. in the .env file you should have key value separate by equal sign. As example:

secret=foo
DATABASE_URL=bar:[email protected]

As the documentation states: https://www.npmjs./package/dotenv

You have not used template literals for mongoose connections.

Try this:

Mongoose.connect(${process.env.DATABASE_URL}, { useNewUrlParser: true });

to get actual .env variable to your Javascript snippet.

I made this mistake, and you shouldn't do it either.

Make sure your.env file is in the root. Check the path of .env before proceeding ahead.

npm install dotenv

require("dotenv").config(); //Keep this at top of your file

In .env file, add

ADMIN_ID="mongoose://******************" //YOUR URL ADDRESS WITH PASSWORD

In app.js file,

mongoose.connect(process.env.ADMIN_ID);

It worked for me.

dotenv.config({ path: "config.env" });

    mongoose.connect(process.env.MONGO_URI, {
      useNewUrlParser: true,
    })

use path. it'll work

发布评论

评论列表(0)

  1. 暂无评论