I am doing a MERN tutorial and when I ran a Web Service Deploy in Render I got 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. Please help me, using simple steps, fix this issue. Thanks
Hello @John_Megill1 welcome to the forums!
Mongoose is not a package provided by MongoDB, I advise you to use our native NodeJS driver
It’s a bit hard to help without you linking to a repo or showing what code you’ve created.
Here is the index.ts file maybe this will help: Preformatted textimport express, { Request, Response } from ‘express’;
import cors from ‘cors’;
import “dotenv/config”;
import mongoose from ‘mongoose’;
import userRoutes from ‘./routes/users’;
import authRoutes from ‘./routes/auth’;
import cookieParser from ‘cookie-parser’;
import path from ‘path’;
import { v2 as cloudinary } from ‘cloudinary’;
import myHotelRoutes from ‘./routes/my-hotels’;
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
})
mongoose.connect(process.env.MONGODB_CONNECTION_STRING as string);
const app = express();
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cors({
origin: process.env.FRONTEND_URL,
credentials: true,
}));
app.use(express.static(path.join(__dirname, “…/…/frontend/dist”)));
app.use(‘/api/auth’, authRoutes);
app.use(‘/api/users’, userRoutes);
app.use(‘/api/my-hotels’, myHotelRoutes);
app.get(“*”, (req: Request, res: Response) => {
res.sendFile(path.join(__dirname, “…/…/frontend/dist/index.html”));
});
app.listen(8000, () => {
console.log(“server running on localhost:8000”);
});Preformatted text
From the look of the code you’re not got environment variables setup for the code to pickup and use.
You’ll need to check how to deploy environment variables to the hosting you’re running it from.
Thanks for this info. It helped and I was able to solve that problem. I now have another error when I deploy. Right now I’m getting this error: MongooseServerSelectionError: Could not connect to any servers in your MongoDB Atlas cluster. One common reason is that you’re trying to access the database from an IP that isn’t whitelisted. Make sure your current IP address is on your Atlas cluster’s IP whitelist:. I’ve added the IP address to cover ‘everywhere’ with the address being 0.0.0.0/0. I’m still not connecting. Would you have any ideas on this?
Yes 
By default Atlas will block external access so you need to whitelist the IP address that you want to talk to it from. Out of the box, it’s pretty friendly in terms of working out your current IP you’re accessing the UI from and adding that, which will probably be your dev machine.
If you access it from another location, say a hosting provider then you need to tell Atlas about what IP range will be making connections and add either an individual IP address or the range that it could come from.
If you search the forums you’ll see a number of posts about setting up the whitelist but the key thing will be working out the IP address your hosting provider is using to talk to Atlas.
You could for testing add the address listing in the message, 0.0.0.0/0 which will allow any client on the internet to talk to your Atlas instance. If you do this make sure your security is locked down and only do it briefly for testing. (strong passwords etc)