简体   繁体   中英

Strapi Heroku deployment fails to connect to MongoDB

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! backend@0.1.0 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM