Render Web Service Deploy

I was running a ‘Web Service Deploy’ on Render and 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. I’ve checked my code and ran tests through the terminal but it all checks out. I was wondering if someone could help me with this problem. Thanks.

Well judging by what you said, you didn’t actually apply a string to Mongoose.

This is wild conjecture but you are probably dotenv. If so, don’t forget to call the config() method on the imported package.

If you aren’t using dotenv, I’m gonna need to see some code.

Here’s my index.ts file:

import 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");
});

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Use their interface.

OK - thank you for that info.

Thanks for the info. I read over the Environment Variables and Secrets but I don’t see anything that directly covers my issue. Could you be more specific, please.

Because you do not (and should not) commit env files, they are not part of your deployed code. As such, Render can’t read from an env file in the code at runtime.

You can use their interface to set the environment variables. After you have set them, you access them at runtime in the code as you would normally (process.env).

Thank you for this info. It made me think of something and I was able to fix the error. However, another error came up: 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’m currently checking into this error.

Use the Atlas UI part of the docs. Set the allow access to 0.0.0.0/0 for all IPs.

Thanks for this info. I’ve allowed access to 0.0.0.0/0 for all IPs and I still get the same error. I am checking into your info and I will keep you posted. Thanks again!

Render also have docs on it. But I don’t think it really contains anything new.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.