【问题标题】:Typescript knex : Cannot read property 'client' of undefinedTypescript knex:无法读取未定义的属性“客户端”
【发布时间】:2020-10-05 04:45:55
【问题描述】:

我的 knex 文件:

import knex from 'knex';
import path from 'path';
const dotenv = require('dotenv').config();

interface KnexConfig {
  [key: string]: object;
}

const config: KnexConfig = {
  developement: {
    client: 'pg',
    connection: {
      host: process.env.DB_HOST,
      user: process.env.DB_USER,
      password: process.env.DB_PASS,
      database: process.env.DB_NAME,
    },
    pool: {
      min: 2,
      max: 10,
    },
    migrations: {
      directory: path.resolve(__dirname, 'src', 'infra', 'knex', 'migrations'),
    },
    timezone: 'UTC',
    useNullAsDefault: true,
  },
};
const KnexInstance = knex(config['development'] as knex.Config);
module.exports = KnexInstance;

我的迁移目录是:src/infra/knex/migrations: 我的文件夹结构:

我的 tsconfig:

{
  "compilerOptions": {
    "target": "es2020",
    "module": "CommonJS",
    "allowJs": true,
    "outDir": "./dist",
    "rootDir": "./src",
    "strict": true,
    "strictPropertyInitialization": false,
    "moduleResolution": "node",
    "baseUrl": "./src",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "forceConsistentCasingInFileNames": true,
    "noImplicitAny": true,
    "typeRoots": ["node_modules/@types", "@types"],
    "paths": {
      "@modules/*": ["modules/*"],
      "@infra/*": ["infra/*"],
      "@config/*": ["config/*"]
    }
  },
  "include": ["src", "__tests__"],
  "exclude": ["node_modules"]
}

我在 yarn knex 上收到此错误:

yarn knex migrate:make add_custom_functions

错误:

需要外部模块 ts-node/register (node:10600) UnhandledPromiseRejectionWarning:TypeError:无法读取属性 未定义的“客户” 在 Object.Knex [默认] (C:\Users\spiriT\ms-emasa\node_modules\knex\lib\knex.js:22:42)

【问题讨论】:

  • "发展" != "发展"

标签: typescript knex.js


【解决方案1】:

您在以下行中有拼写错误,导致config.development 未定义。可能值得使用点访问器语法而不是方括号访问器来让您的编辑器选择这样的错别字。

  developement: {

【讨论】:

  • 什么?我不明白我有同样的发展:{}你能更具体吗?
  • 仔细观察“发展”这个词的拼写。
猜你喜欢
  • 2023-04-10
  • 2020-09-22
  • 1970-01-01
  • 1970-01-01
  • 2021-04-12
  • 2022-01-09
  • 1970-01-01
  • 2021-11-21
  • 2022-01-22
相关资源
最近更新 更多