【问题标题】:Typescript and typeorm config .ts no overload matches this callTypescript 和 typeorm config .ts 没有重载匹配这个调用
【发布时间】:2020-07-21 13:55:42
【问题描述】:

您好,我的 app.ts 有问题 从我的 ormconfig.ts 为 typeorm 函数加载我的设置以创建连接时 我的应用出现此错误:

No overload matches this call.
  Overload 1 of 3, '(name: string): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'string'.
  Overload 2 of 3, '(options: ConnectionOptions): Promise<Connection>', gave the following error.
    Argument of type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is not assignable to parameter of type 'ConnectionOptions'.
      Type 'typeof import("d:/EmasaTi_StockControl/src/shared/infra/http/ormconfig")' is missing the following properties from type 'ExpoConnectionOptions': type, database, driverts(2769)
Peek Problem (Alt+F8)
No quick fixes available

代码:

import 'dotenv/config';
import { createConnection } from 'typeorm';
import App from './app';
import validateEnv from '@utils/validateEnv';
import * as config from './ormconfig';
validateEnv();

(async () => {
  try {
    const connection = await createConnection(config);
    await connection.runMigrations();
  } catch (error) {
    console.log('Error while connecting to the database', error);
    return error;
  }
  const app = new App(
  );
  app.listen();
})();

我的配置文件:

import { ConnectionOptions } from 'typeorm';

const rootDir = process.env.NODE_ENV === 'development' ? 'src' : 'build/src';
export const config: ConnectionOptions = {
  type: 'postgres',
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASS,
  database: process.env.DB_NAME,
  entities: [rootDir + '/entities/**/*.{js,ts}'],
  migrations: [rootDir + '/migrations/*.{js,ts}'],
  subscribers: [rootDir + '/subscribers/**/*.{js,ts}'],
  cli: {
    entitiesDir: `${rootDir}/entities`,
    migrationsDir: `${rootDir}/migration`,
    subscribersDir: `${rootDir}/subscriber`,
  },
  synchronize: false,
  logging: true
};

module.exports = config;

我无法想象如何解决这个问题 如果有人可以帮助我,我将不胜感激

【问题讨论】:

  • 如果你在createConnection({...}) 内联设置配置对象而不导入它是否有效?
  • 请在那里查看我的回答。我为 postgres 编写了示例工作配置,可能会对您有所帮助:stackoverflow.com/questions/63678216/…

标签: typescript typeorm


【解决方案1】:

我不确定完美的解决方案是什么,但我找到了解决方法。

  1. 视情况从您的tsconfig.json 或其他配置文件中删除"type": "mysql"
  2. 照常导入您的配置。示例:import config from './ormconfig.json'
  3. 像这样使用config 变量:createConnection({type: "postgres", ...config})

我认为由于某些原因,type 键没有被正确解析。

干杯。

【讨论】:

  • 这对我有用。谢谢
猜你喜欢
  • 1970-01-01
  • 2020-06-14
  • 2021-12-19
  • 1970-01-01
  • 2022-06-11
  • 2021-10-12
  • 2021-08-16
  • 1970-01-01
  • 2020-09-13
相关资源
最近更新 更多