Deploy Error MongoDB Heroku

I can’t deploy to heroku with my full stack app. I am sure it has to do with mongodb not connecting properly. I am shown 2 errors.

MongooseError: Operation users.findOne() buffering timed out after 10000ms

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.

In my code I connect to mongodb using the url I obtained when I created a user on my db in the cluster. I am sure I am missing a small detail to connect.


const express = require("express");

const app = express();
const PORT = process.env.PORT || (my default port number)
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const helmet = require("helmet");
const morgan = require("morgan");
const cors = require("cors");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const errorHandler = require("./middleware/error");
const path = require("path");

const Routes = require('./routes/routes');


//code I use to connect
mongoose.connect(process.env.MONGO_URL, {useNewUrlParser: true, useUnifiedTopology:true}).then(() => {
    console.log("MongoDB Connected")
.catch((err) => console.log(err));

app.use("/api", Routes);

//Error Middleware

app.use(express.static(path.join(__dirname, "/frontend/build")));

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, '/frontend/build', 'index.html'));

app.listen(PORT, ()=> {
    console.log("Backend server is running!")

Are you using the config vars?

Yeah I am using the config variables and I solved the issue. I misspelled one of the config variables supposed to be MANGO_URL and I added this code to my path

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

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