Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mikro orm config error key is null #866

Closed
AndonMitev opened this issue Sep 24, 2020 · 13 comments
Closed

Mikro orm config error key is null #866

AndonMitev opened this issue Sep 24, 2020 · 13 comments
Assignees
Labels
question Further information is requested

Comments

@AndonMitev
Copy link

AndonMitev commented Sep 24, 2020

Describe the bug
I'm trying to setup mikro orm and connect to postgresql but following error pop up:

TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received null

Stack trace
[discovery] ORM entity discovery started, using ReflectMetadataProvider
[discovery] - processing entity Post
[discovery] - entity discovery finished, found 1 entities, took 12 ms
internal/crypto/keys.js:304
throw new ERR_INVALID_ARG_TYPE(
^

TypeError [ERR_INVALID_ARG_TYPE]: The "key" argument must be of type string or an instance of Buffer, TypedArray, DataView, or KeyObject. Received null
at prepareSecretKey (internal/crypto/keys.js:304:11)
at new Hmac (internal/crypto/hash.js:113:9)
at Object.createHmac (crypto.js:143:10)
at createHMAC (C:\Users\earth\Desktop\FullStack\node_modules\pg\lib\sasl.js:133:17)
at Hi (C:\Users\earth\Desktop\FullStack\node_modules\pg\lib\sasl.js:137:13)
at Object.continueSession (C:\Users\earth\Desktop\FullStack\node_modules\pg\lib\sasl.js:32:24)
at Client._handleAuthSASLContinue (C:\Users\earth\Desktop\FullStack\node_modules\pg\lib\client.js:248:10)
at Connection.emit (events.js:315:20)
at Connection.EventEmitter.emit (domain.js:483:12)
at C:\Users\earth\Desktop\FullStack\node_modules\pg\lib\connection.js:109:12 {
code: 'ERR_INVALID_ARG_TYPE'
}
[nodemon] app crashed - waiting for file changes before starting...

Mikro ORM Config

export default { migrations: { path: path.join(__dirname, "./migrations"), pattern: /^[\w-]+\d+\.[tj]s$/, }, entities: [Post], dbName: "new-stuff", type: "postgresql", debug: !__prod__, } as Parameters<typeof MikroORM.init>[0];

@AndonMitev AndonMitev added the bug Something isn't working label Sep 24, 2020
@AndonMitev AndonMitev changed the title Mikro orm config Mikro orm config error Sep 24, 2020
@AndonMitev AndonMitev changed the title Mikro orm config error Mikro orm config error key is null Sep 24, 2020
@B4nan B4nan added question Further information is requested and removed bug Something isn't working labels Sep 24, 2020
@B4nan
Copy link
Member

B4nan commented Sep 24, 2020

Not sure what is wrong, but this has nothing to do with MikroORM, the error is thrown from pg, most probably you have the connection misconfigured (or your postgres server, hard to say). Maybe you need to provide password or something like that?

@B4nan
Copy link
Member

B4nan commented Sep 24, 2020

Looking at the source code of pg, and the createHMAC is called with the password.

What versions are you using? How does your package.json look like? Are you able to connect to your database with 3rd party tool (like pg-admin, phpmyadmin, adminer, etc...)?

@AndonMitev
Copy link
Author

image
Yes i can see the db in 3rd party tool. I guess might be something with the password then! Thanks a lot

@B4nan
Copy link
Member

B4nan commented Sep 24, 2020

How does the full config look like? Maybe you are using wrong fields (e.g. hostname instead of host, or username instead of user), the interface looks like this:

export interface ConnectionOptions {
  dbName?: string;
  name?: string;
  clientUrl?: string;
  host?: string;
  port?: number;
  user?: string;
  password?: string;
  charset?: string;
  multipleStatements?: boolean; // for mysql driver
  pool?: PoolConfig;
}

Doing export default { ... } as Parameters<typeof MikroORM.init>[0] is also not ideal, as it won't type check things correctly. Better to do it this way:

import { Options } from '@mikro-orm/core'; // `Options` is what you want, no need to get the type from init method

const config: Options = { ... };

export default config;

@AndonMitev
Copy link
Author

AndonMitev commented Sep 25, 2020

Well this is how my config looks like:

const config: Options = { migrations: { path: path.join(__dirname, "./migrations"), pattern: /^[\w-]+\d+\.[tj]s$/, }, entities: [Post], dbName: "postgres", type: "postgresql", debug: !__prod__, };

@B4nan
Copy link
Member

B4nan commented Sep 25, 2020

Can you share (for future readers) what was the issue? I guess you were missing a password in your config, and it was required by your postgres server?

@akrabdev
Copy link

akrabdev commented Oct 1, 2020

not OP, had the exact same error when I omitted "password" from config, As mentioned it has nothing to do with Micro-orm

@sleepybyte23
Copy link

sleepybyte23 commented Nov 2, 2020

import { Options } from '@mikro-orm/core';

const config: Options = {
  migrations: {
    path: path.join(__dirname, "./migrations"), // path to the folder with migrations
    pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files       
  },
  entities: [Post],
  dbName : 'lireddit',
  type: 'postgresql',
  password: "s3xy",
  debug: !__prod__,
};

export default config;

worked for me, I was missing password.

@omdxp
Copy link

omdxp commented Jan 14, 2022

@sleepybyte23 but when I tried to put the password as a env variable by using process.env.PASSWORD! for example I get the same issue

@Langstra
Copy link
Collaborator

Unsure what you are exactly trying, without any context it is hard to tell. If you are trying environment variables please check the documentation. https://mikro-orm.io/docs/configuration/#using-environment-variables

For other questions you could use the discussion feature on GitHub, try the mikroorm slack or stackoverflow. GitHub issues are for bugs, improvements or feature requests, not for support. If you truly believe you found a bug, then open a new issue with a full bug report, issue template and reproduction.

@omdxp
Copy link

omdxp commented Jan 14, 2022

Unsure what you are exactly trying, without any context it is hard to tell. If you are trying environment variables please check the documentation. https://mikro-orm.io/docs/configuration/#using-environment-variables

For other questions you could use the discussion feature on GitHub, try the mikroorm slack or stackoverflow. GitHub issues are for bugs, improvements or feature requests, not for support. If you truly believe you found a bug, then open a new issue with a full bug report, issue template and reproduction.

@Langstra I tried to put the password in .env file like this:

PASSWORD=test

And use it in the config file like this:

import { Options } from '@mikro-orm/core';

const config: Options = {
  migrations: {
    path: path.join(__dirname, "./migrations"), // path to the folder with migrations
    pattern: /^[\w-]+\d+\.[tj]s$/, // regex pattern for the migration files       
  },
  entities: [Post],
  dbName : 'lireddit',
  type: 'postgresql',
  password: process.env.PASSWORD,
  debug: !__prod__,
};

export default config;

And I get this:

error: sasl: scram-server-first-message: client password must be a string

@B4nan
Copy link
Member

B4nan commented Jan 14, 2022

You need to initialize dotenv in that config file if you want to use it like this (the ORM does this internally, but at a later stage, after the CLI config is loaded). Or just use the ORM env var, which is MIKRO_ORM_PASSWORD.

@omdxp
Copy link

omdxp commented Jan 14, 2022

You need to initialize dotenv in that config file if you want to use it like this (the ORM does this internally, but at a later stage, after the CLI config is loaded). Or just use the ORM env var, which is MIKRO_ORM_PASSWORD.

Thanks for the info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

6 participants