14

I created a Strapi app using the latest version, the new 4.0 and I wanted to deploy it to Heroku. I did follow the Strapi documentation in order to do so, like explained in this page. Now I'm getting an error that I don't understand, I guess it has something to do with postgres. This is the error

2021-12-18T15:26:26.658380+00:00 app[web.1]: [2021-12-18 15:26:26.656] debug: ⛔️ Server wasn't able to start properly.
2021-12-18T15:26:26.659122+00:00 app[web.1]: [2021-12-18 15:26:26.658] error: Unknow dialect undefined
2021-12-18T15:26:26.659123+00:00 app[web.1]: Error: Unknow dialect undefined
2021-12-18T15:26:26.659123+00:00 app[web.1]: at getDialectClass (/app/node_modules/@strapi/database/lib/dialects/index.js:12:13)
2021-12-18T15:26:26.659123+00:00 app[web.1]: at getDialect (/app/node_modules/@strapi/database/lib/dialects/index.js:19:23)
2021-12-18T15:26:26.659124+00:00 app[web.1]: at new Database (/app/node_modules/@strapi/database/lib/index.js:38:20)
2021-12-18T15:26:26.659124+00:00 app[web.1]: at Function.Database.init (/app/node_modules/@strapi/database/lib/index.js:84:33)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at Strapi.bootstrap (/app/node_modules/@strapi/strapi/lib/Strapi.js:347:30)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at Strapi.load (/app/node_modules/@strapi/strapi/lib/Strapi.js:410:16)
2021-12-18T15:26:26.659125+00:00 app[web.1]: at async Strapi.start (/app/node_modules/@strapi/strapi/lib/Strapi.js:161:9)

Apart from doing what is explained in the docs I linked, I just added a few collections using the UI in development mode. How can I fix this error and deploy to Heroku this new 4.0 version of Strapi?

ouflak
  • 2,458
  • 10
  • 44
  • 49
Essay97
  • 648
  • 1
  • 9
  • 24

9 Answers9

15

I had a similar issue when I was connecting pg locally and then realised my connection config was incorrect. When I replaced it with v4 template it worked.

path: config/database.js

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connection: {
    client: "postgres",
    connection: {
      host: env("DATABASE_HOST", "localhost"),
      port: env.int("DATABASE_PORT", 5432),
      database: env("DATABASE_NAME", "bank"),
      username: env("DATABASE_USERNAME", "postgres"),
      password: env("DATABASE_PASSWORD", "0000"),
      schema: env("DATABASE_SCHEMA", "public"),
    },
  }
});

And for Heorku as the article suggested:

path: config/env/production/database.js:

const parse = require('pg-connection-string').parse;
const config = parse(process.env.DATABASE_URL);
module.exports = ({ env }) => ({
  connection: {
    client: 'postgres',
    connection: {
      host: config.host,
      port: config.port,
      database: config.database,
      user: config.user,
      password: config.password,
      ssl: {
        rejectUnauthorized: false
      },
    },
    debug: false,
  },
});

PS: make sure you add pg-connection-string to your dependencies before pushing to heroku

Dharman
  • 30,962
  • 25
  • 85
  • 135
iveedee
  • 572
  • 1
  • 5
  • 15
1

getDialectClass - from the error log

const getDialectClass = client => {
  switch (client) {
    case 'postgres':
      return require('./postgresql');
    case 'mysql':
      return require('./mysql');
    case 'sqlite':
      return require('./sqlite');
    default:
      throw new Error(`Unknow dialect ${client}`);
  }
};

where the client is - db.config.connection

So if you have been following previous solutions - which talked about a connections object etc ( like this )

module.exports = ({ env }) => ({
  defaultConnection: 'default',
  connections: {
    default: {
      connector: 'bookshelf',
      settings: {
        client: 'postgres',
        host: config.host,
        port: config.port,
        database: config.database,
        username: config.user,
        password: config.password,
        ssl: {
          rejectUnauthorized: false,
        },
      },
      options: {
        ssl: true,
      },
    },
  },
});

db.config.connection would return undefined. & so it would fail

Configuring the DB with - https://docs.strapi.io/developer-docs/latest/setup-deployment-guides/configurations/required/databases.html#configuration-structure Works fine

Roshan Khandelwal
  • 953
  • 1
  • 11
  • 17
  • Also with this update, With v4 - Running complex queries would update to - `await strapi.db.connection.raw( ' **** ' ) `FROM `await strapi.connections.default.raw ( ' **** ' )` – Roshan Khandelwal Jan 06 '22 at 05:53
1

If you are using typescript in Strapi, we may encounter this issue too as there is a issue affecting Strapi typescript capabilities.

I can resolve the problem when I downgraded Strapi to 4.3.2 from 4.3.8

Tsquare
  • 21
  • 6
0

You can check this method using heroku/cli from strapi docs here, but it's the same thing, i'm using version 4.0.2, this methods works on older versions V3, i think the docs needs update, most of it is deprecated, for example "fetching only data (example: posts/project etc..) related to the authenticated user who created it", most of resources related to this topic in docs and forum are deprecated solutions that don't work in V4.

For the deployment, i have the same issue, i tried a lot of solutions but it didn't work for me to, but i managed to get the server running like this:

enter image description here

but i got this problem when i visit the "/admin" page:

enter image description here

Strapi is really interesting, let's hope they update the docs soon and someone respond with a solution here.

0

I fixed this issue by using database url directly. Not sure why the old way does not work in Strapi v4.

module.exports = ({ env }) => ({
  defaultConnection: "default",
  connection: {
    client: "postgres",
    connection: {
      connectionString: process.env.DATABASE_URL,
      ssl: {
        rejectUnauthorized: false
      }
    },
  }
});
yeMarn
  • 36
  • 2
0

In case someone meets the same problem as me.

In my situation, I am using vim in windows as my text editor. After editing the "config/database.js" file. It left a "database.js~" file in that directory. Then, this filename causes the same problems: "Error: Unknown dialect undefined".

I guess it's a bug in strapi. After removing that file, everything works!

So, the solutions for me. I add this line to my vim config file:

set backupdir=~/.vimbackup

Then create a directory named '.vimbackup' in "C:/User/[me]/" .

欧阳维杰
  • 1,608
  • 1
  • 14
  • 22
0

Strapi has once again changed the database configuration structure and even their deployment docs use the old and incorrect example.

Here is the most up to date example: Strapi Database Configuration

Lauris
  • 345
  • 2
  • 8
0

In strapi v4, you need to use like:

module.exports = ({ env }) => ({
  connection: {
    client: "postgres",
    connection: {
      host: env("DATABASE_HOST", "localhost"),
      port: env.int("DATABASE_PORT", 5432),
      database: env("DATABASE_NAME", "db-name"),
      user: env("DATABASE_USERNAME", "postgres"),
      password: env("DATABASE_PASSWORD", "password"),
      schema: env("DATABASE_SCHEMA", "public"),
    },
  }
});
Drashti Kheni
  • 1,065
  • 9
  • 23
0

it's look like there is a problem in the new strapi version , when i try to create project with typescript support i faced same error , i don't know if you are using typescript in your project but these two solutions solved my problem;

solution (1) :- create compile config ts files with tsc $ tsc .\config\database.ts .\config\server.ts ./ .\config\admin.ts

solution (2) :- downgrade your strapi to 4.3.2 version in the package.json file.

solution (3) :- don't use @latest when creating strapi app instate use 4.3.2 npx create-strapi-app@4.3.2 test-strapi-verion-with-ts --ts

you can use one of theme

kadhum alrubaye
  • 151
  • 1
  • 7