0

I have an Express Mongoose app that used to connect to MongoDB Atlas without any problem whatsoever.

It is hosted on Repl.it

But when I checked today, it is not working.

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.

Nothing has changed from last time, 21 days ago.

The packages are all the latest version.

Here is the code.

require('dotenv').config();
const express = require('express');
const cors = require('cors');
const db = require("mongodb")
const mongoose = require('mongoose')
const bodyParser = require('body-parser')

const port = process.env.PORT || 3000;
const uri = process.env.MONGO_URI;
mongoose.connect(uri, {
  useNewUrlParser:true,
  useUnifiedTopology:true,
  serverSelectionTimeoutMS:6000

})

const connection = mongoose.connection;
connection.on('error', console.error.bind(console, 'connection error'));
connection.once('open', () => {
  console.log("Connected successfully")
});

app.listen(port, () => {
  console.log(`Server is running on port : ${port}`);
})

What could be the issue?

Any idea would be highly appreciated.

Thank you.

vinod-vms
  • 25
  • 6
  • I risked taking the connection string from the .env file and pasting it into the mongoose.connect() function. It works!!! Presumably it is a problem with the 'dotenv' package. But it worked 21 days ago. Why it is not working now??? – vinod-vms Jan 08 '21 at 15:04
  • What could have happened to .dotenv, or repl.it, in 21 days??? The problem is still unresolved. It is not connecting. – vinod-vms Jan 08 '21 at 15:25

1 Answers1

0

In repl.it, there is a 'Run' button, with a green 'Play' icon, against the project name.

You have to click on that to run the project.

For me, a 'node server.js' or 'npm start' shall not work!

This issue has been resolved.

Thank You.

vinod-vms
  • 25
  • 6