0
votes

I am trying to deploy my Strapi app to Heroku with a mongoDB but receive this error every time I try to deploy.

debug ⛔️ Server wasn't able to start properly.
2020-06-11T10:38:53.257748+00:00 app[web.1]: [2020-06-11T10:38:53.257Z] error Error connecting to the Mongo database. Server selection timed out after 30000 ms
2020-06-11T10:38:53.268085+00:00 app[web.1]: npm ERR! code ELIFECYCLE
2020-06-11T10:38:53.268583+00:00 app[web.1]: npm ERR! errno 1
2020-06-11T10:38:53.270443+00:00 app[web.1]: npm ERR! [email protected] start: `strapi start`
2020-06-11T10:38:53.270697+00:00 app[web.1]: npm ERR! Exit status 1

The app works locally and I have followed numerous tutorials (1, 2), including many versions of the offical Strapi documentation in order to get this working but they all result in this error. I have looked at similar Stack questions / Strapi github Issues to see if my issue can be solved by adapting their answers but nothing works.

I have ensured my heroku config database_url and database_name matches my database_uri string, and I created the strapi app with yarn create strapi-app backend then selected mongo, making sure not to use --quickstart.

My full strapi app, including database.js, server.js, *config/environments/production/database.json can be viewed here https://github.com/KyleSFraser/Portfolio/tree/master/backend

I've been going in circles constantly rebuilding the backend using the docs or tutorials trying to deploy. I'd really appreciate any pointers to solve this recurring issue and successfully deploy my mongoDB using Heroku & Strapi.

EDIT Removing previous config vars now that it is working

1
Did you set all the DATABASE_ env vars in your Heroku app? - Maxim Orlov
Hey Maxim, yeah my DATABASE_ env vars are all set in the Heroku app, I've added a screenshot of this and theDATABASE_URI to the originally question. I was sure I set these up correctly but if you notice anything incorrect please let me know - fraserky
I also get a timeout error when I try to connect to that DB. Double check your DB is up and running with mLab. You said it's working locally, are you connecting to mLab or a local MongoDB? - Maxim Orlov
Hmm that's weird. Everything else looks good to me. Try to set DATABASE_SSL=true in Heroku to match "ssl": true from database.json. If that still doesn't work then I'm not really sure what the problem could be. Maybe someone else can spot something. - Maxim Orlov
I gave it a shot but no success unfortunately, thanks very much for taking the time to help me out though, it's really appreciated! Hope you have a good day and weekend! - fraserky

1 Answers

1
votes

Adding uri: env('DATABASE_URI' || ''), to my database.js file has solved the issue, not sure why it was missing in the first place.

For anyone with a similar issue, here is my database.js and server.js for reference;

database.js

  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'mongoose',
      settings: {
        uri: env('DATABASE_URI' || ''),
        host: env('DATABASE_HOST', '127.0.0.1'),
        srv: env.bool('DATABASE_SRV', false),
        port: env.int('DATABASE_PORT', 27017),
        database: env('DATABASE_NAME', '(backend)'),
        username: env('DATABASE_USERNAME', ''),
        password: env('DATABASE_PASSWORD', ''),
      },
      options: {
        authenticationDatabase: env('AUTHENTICATION_DATABASE', null),
        ssl: env.bool('DATABASE_SSL', false),
      },
    },
  },
});

server.js

module.exports = ({ env }) => ({
  host: env('HOST', '0.0.0.0'),
  port: env.int('PORT', 1337),
});

Finally make sure all you Heroku config vars are set to match the relevant breakdown of your mongoDB/database URI