I'm trying to deploy a node app to Render, but keep getting this error message: 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.
After searching on here for a while and trying different things, I'm still getting the same result and I don't really know why. My connection works locally, but when I try to deploy it fails.
const express = require('express');
const mongoose = require('mongoose');
require('dotenv').config({path:__dirname '/.env'});
const cors = require('cors')
const quizRoutes = require('./router/quiz');
const articleRoutes = require('./router/article');
const app = express();
//middleware
app.use(express.json());
app.use(cors());
app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
app.use('/quiz', quizRoutes);
app.use('/articles', articleRoutes);
mongoose.connect(process.env.MONGO_URI)
.then(() => {
app.listen(process.env.PORT, () => {
console.log(process.env)
console.log('Port', process.env.PORT);
})
})
.catch((error) => {
console.log(error);
})
PORT=4000
MONGO_URI="mongodb srv://personalinfo.lahdouv.mongodb.net/?retryWrites=true&w=majority"
Changed Mongo_URI for obvious reasons, but that is the code I'm working with.
I tried changing the .env config, putting the URI in quotes, and trying to make sure that the .env file was being linked correctly. Locally, I used console.log to make sure, and it still works. Not sure where to go from here.
Any help is greatly appreciated and thank you very much for your time.
CodePudding user response:
Instead of using env file you can try this. There is an extended option in render to add environment variable before the apps starts to be deployed . select advanced option and you will see an option to add environment variable . add variable save and deploy