Strapi Heroku deployment fails to connect to MongoDB

六月ゝ 毕业季﹏ 提交于 2020-06-17 15:49:49

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/62322925/strapi-heroku-deployment-fails-to-connect-to-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!