Home > front end >  Node.js can't recognise any variables in .env
Node.js can't recognise any variables in .env

Time:12-24

I am following the fullstackopen.com course and I seem to have an issue with my .env file, currently when I try to connect to the database I get this error:

error connecting to MongoDB The `uri` parameter to `openUri()` 
must be a string, got "undefined". Make sure the first parameter to 
`mongoose.connect()` or `mongoose.createConnection()` is a string.

I have figured out the process.env variable is not being read properly by Node.js from checking previous answers most of the issue has been around not having dotenv imported correctly my code has this so I don't think this could be the issue. I have also printed the .env variable to console and it is undefined. My .env file is also in the root of the project so I don't think it is that either.

I have included my .env file and the files being used to call the code below.

.env file

MONGODB_URI='mongodb srv://fullstackopen:<MyPasswordisHERE>@cluster0.brwcy.mongodb.net/myFirstDatabase?retryWrites=true&w=majority'
PORT=3001

note.js application

require('dotenv').config()
const mongoose = require('mongoose')
const url = process.env.MONGODB_URI
console.log('connecting to', url)

mongoose.connect(url)
    .then(result => {
        console.log('connected to MongoDB')
    })
    .catch((error) => {
        console.log('error connecting to MongoDB', error.message)
    })

const noteSchema = new mongoose.Schema({
    content: String,
    date: Date,
    important: Boolean,
})

noteSchema.set('toJSON', {
    transform: (document, returnedObject) => {
        returnedObject.id = returnedObject._id.toString()
        delete returnedObject._id
        delete returnedObject.__v
    }
  })
  
module.exports = mongoose.model('Note', noteSchema)

index.js

require('dotenv').config()
const { request, application, response } = require('express')
const express = require('express')
const app = express()
const Note = require('./models/note')
app.use(express.json())
app.use(express.static('build'))


const cors = require('cors')
app.use(cors())


  app.get('/', (request, response) => {
    response.send('<h1>Hello World</h1>')
  })

  app.get('/api/notes/:id', (request, response) => {
      const id = Number(request.params.id)
      const note = notes.find(note => note.id === id)
      if(note){
        response.json(note)
      }
      else {
        response.status(404).end()
      }
    })

  app.get('/api/notes',(request, response) => {
      Note.find({}).then(notes => {
        console.log(response)
        response.json(notes)
      })
  })

  app.delete('/api/notes/:id', (request, response) => {
    const id = Number(request.params.id)
    notes = notes.filter( note => note.id !== id)

    response.status(204).end()
  })

  const generateId = () => {
    const maxId = notes.length > 0 
    ? Math.max(...notes.map(n => n.id))
    : 0

    return maxId   1
  }

  app.post('/api/notes', (request, response) => {
   
    const body = request.body

    if(!body.content){
      return response.status(400).json({
        error: 'content missing'
      })
    }

    const note = {
      content: body.content,
      important: body.important || false,
      date: new Date(),
      id: generateId(),
    }
    
    notes = notes.concat(note)
    response.json(note)


  })

  const unknownEndpoint = (request, response) => {
    response.status(404).send({error: 'unknown endpoint'})
  }

  app.use(unknownEndpoint)

  const PORT = process.env.PORT
  app.listen(PORT, ()=> {
      console.log(`Sever is running on port ${PORT}`)
  })


I know that I have dotenv being imported in note.js and index.js, the reason for this is when I was testing why the .env was't being recognised I checked the note.js file by running that only using the command below, however in production the import is only in index.js so that isn't the issue

node note.js

My file structure for the project is also included below

.  ..  build  .env  .git  .gitignore  index.js  models  mongo.js  node_modules  package.json  package-lock.json  Procfile  requests

CodePudding user response:

Make sure that your .env is within the folder structure. E.g if your .env is on the root folder but you are trying to load it from within a folder make sure you add the correct path:

require('dotenv').config({path: __dirname   '/.env' })

CodePudding user response:

Figured out the issue, when deploying with heroku you must configure the config vars to match the environment variables in .env

The other answers in StackOverflow aren't so clear on how to do this, I have outlined the steps I took below.

  1. Go to Application > Settings > Reveal config vars
  2. you will be presented with two text fields one labelled key and the other value
  3. For the key make it equal the name of your environment variable for me it was MONGODB_URI
  4. For the value field it should equal whatever you need your environment variable to be, for me it was the url for MongoDB Atlas.
  • Related