I'm trying to deploy an app on heroku but i can't if i use dotenv. This code doesn't work:
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true }, () => {
console.log("Connected to db successfully");
});
But this does:
mongoose.connect("mongodb+srv://test:test@cluster0.j0you.mongodb.net/ecommerce?retryWrites=true&w=majority", { useNewUrlParser: true }, () => {
console.log("Connected to db successfully");
});
When i try to use dotenv heroku throws this error:
MongooseError: 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 had to go to heroku dashboard> settings> config vars and then add the key as MONGO_URI and the mongo url value that mongodb gave me
You need to make sure of two things