0

This is my code for mongodb atlas connection, it shows connection successful but later when I click on some tab the app crashes. I m deploying app through heroku and i have given the error below .app runs fine on localhost but crashes on the heroku website. It sometimes allows and opens some pages but crashes right after that , i have tried fixing environemnt variables on heroku website but that was of no help so i switched to just using the mongo connection link through the variable. Any help is appreciated. I discovered that the server throws the error of not being able to connect after exactly 30 seconds but still cant find what is causign the error

if (process.env.NODE_ENV !== "production") {
    require('dotenv').config();
}

const sanitize = require('express-mongo-sanitize');
// const dbURL = process.env.DB_URL;
const session = require('express-session');
const MongoStore = require('connect-mongodb-session')(session);
const dbURL = "mongodb+srv://RohanGupta:****************@*****.****.mongodb.net/oddevegame?retryWrites=true&w=majority";

mongoose.connect(dbURL, {
    useNewUrlParser: true,
    useCreateIndex: true,
    useUnifiedTopology: true,
    useFindAndModify: false
})
    .then(() => {
        console.log("Connected Mongo Successfully")
    })
    .catch(err => {
        console.log("Uh Oh couldnt connect mongo!!")
        console.log(err);
    })


app.use(methodOverride('_method'));
app.use(express.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, '/public')));
app.use(express.static(path.join(__dirname, '/resources')));



app.use(session({
    store: new MongoStore({
        url: dbURL,
        touchAfter: 24 * 3600
    }),
    name: "blah",
    secret: 'this is secret',
    resave: false,
    saveUninitialized: false,
    cookie: {
        httpOnly: true,
        // secure: true,
        expires: Date.now() + 1000 * 60 * 60 * 24 * 7,
        maxAge: 1000 * 60 * 60 * 24 * 7
    }
}));

2021-06-20T07:27:23.462279+00:00 heroku[web.1]: State changed from starting to up
2021-06-20T07:27:23.230556+00:00 app[web.1]: Listening on PORT 4000
2021-06-20T07:27:23.502833+00:00 app[web.1]: Connected Mongo Successfully
2021-06-20T07:27:53.221633+00:00 app[web.1]: /app/node_modules/mongodb/lib/utils.js:698
2021-06-20T07:27:53.221654+00:00 app[web.1]:           throw error;
2021-06-20T07:27:53.221655+00:00 app[web.1]:           ^
2021-06-20T07:27:53.221655+00:00 app[web.1]: 
2021-06-20T07:27:53.221656+00:00 app[web.1]: Error: Error connecting to db: connect ECONNREFUSED 127.0.0.1:27017
2021-06-20T07:27:53.221656+00:00 app[web.1]:     at /app/node_modules/connect-mongodb-session/index.js:78:17 



Lavande
  • 744
  • 8
  • 20
  • Maybe do not add your actual DB Conn URL here – kakou Jun 20 '21 at 07:40
  • Check if the heroku server is whitelisted in MongoDB Atlas. Also check if this solves your problem - https://stackoverflow.com/questions/13312358/mongo-couldnt-connect-to-server-127-0-0-127017?rq=1 – Rudr Thakur Jun 20 '21 at 07:46
  • Hi Karolos ,i have tried the method of not typing it in app.js but it still doesnt resolve the error . i have added the server connection in heroku env too – rohan-here Jun 20 '21 at 07:57
  • Hi Rudr , i have gone through the link but it only solves the probelm on local machine while mine error is on connection to the mongodb atlas when using heroku – rohan-here Jun 20 '21 at 07:58
  • i was able to fix it by removing session thnx for help – rohan-here Jun 20 '21 at 09:49

0 Answers0