Egg: Property 'mysql' does not exist on type 'Application'

Created on 5 Jun 2018  ·  5Comments  ·  Source: eggjs/egg

  • Node Version: 8.7.0
  • Egg Version: ^2.6.1
  • Plugin Name: egg-mysql
  • Plugin Version: ^3.0.0
  • Platform: MacOS 10.13.1
  1. 按照官方文档typescript章节利用egg-init初始化ts项目
  2. 安装egg-mysql, 配置config
  3. 在service里使用mysql,编译报错

config配置如下

// config/plugin.ts
const plugin: EggPlugin = {
  // static: true,
  // nunjucks: {
  //   enable: true,
  //   package: 'egg-view-nunjucks',
  // },
  mysql: {
    enable: true,
    package: 'egg-mysql',
  }
};

// config/config.local.ts
export default () => {
  const config: DefaultConfig = {
    mysql: {
      client: {
        host: '127.0.0.1',
        port: '3306',
        user: 'root',
        password: '12345678',
        database: 'test',
      },
      app: true,
      agent: false,
    },
  };
  return config;
};

// config/config.prod.ts
export default () => {
  const config: DefaultConfig = {
    mysql: {
      client: {
        host: '127.0.0.1',
        port: '3306',
        user: 'root',
        password: '12345678',
        database: 'test',
      },
      app: true,
      agent: false,
    },
  };
  return config;
};

service文件

 ...
 public async findById(uid) {
    try {
      // 报错 Property 'mysql' does not exist on type 'Application'
      const user = await this.app.mysql.get('users', { id: uid });
      return { user };
    } catch (err) {
      this.logger.error(err);
      return {};
    }
  }

请问是哪里出问题了。。谢谢

Most helpful comment

目前 egg-mysql 没有写声明,因此在 Application 中是没有 mysql 的声明的,欢迎提个 PR 到 egg-mysql 补充 d.ts

现在临时解决可以在 typings/index.d.ts 下补充声明

declare module 'egg' {
  interface Application {
    mysql: any;
  }
}

All 5 comments

目前 egg-mysql 没有写声明,因此在 Application 中是没有 mysql 的声明的,欢迎提个 PR 到 egg-mysql 补充 d.ts

现在临时解决可以在 typings/index.d.ts 下补充声明

declare module 'egg' {
  interface Application {
    mysql: any;
  }
}

还有egg-redis。。。

还有egg-oss

@superlbr @yuezk PR is welcome

目前 egg-mysql 没有写声明,因此在 Application 中是没有 mysql 的声明的,欢迎提个 PR 到 egg-mysql 补充 d.ts

现在临时解决可以在 typings/index.d.ts 下补充声明

declare module 'egg' {
  interface Application {
    mysql: any;
  }
}

感谢,按照这个方法,问题解决了。

Was this page helpful?
0 / 5 - 0 ratings