midwayjs / midway

🍔 A Node.js Serverless Framework for front-end/full-stack developers. Build the application for next decade. Works on AWS, Alibaba Cloud, Tencent Cloud and traditional VM/Container. Super easy integrate with React and Vue. 🌈
https://www.midwayjs.org/
MIT License
7.34k stars 573 forks source link

@midwayjs/core 无法 inject node_modules 里的 ORM 模型 #800

Closed falstack closed 3 years ago

falstack commented 3 years ago

@midwayjs/core2.5.5版本中我可以将项目的entity放到另一个仓库中注入,如:

2.5.5版本是 work 的,但是升级到2.6.10就会报错:

RepositoryNotFoundError: No repository for "User" was found. Looks like this entity is not registered in current "default" connection?
    at new RepositoryNotFoundError (/Users/yuistack/Documents/calibur/fc/src/error/RepositoryNotFoundError.ts:11:9)
    at EntityManager.getRepository (/Users/yuistack/Documents/calibur/fc/src/entity-manager/EntityManager.ts:919:19)
    at Connection.getRepository (/Users/yuistack/Documents/calibur/fc/src/connection/Connection.ts:346:29)
    at Object.getRepository (/Users/yuistack/Documents/calibur/fc/src/index.ts:284:55)
    at /Users/yuistack/Documents/calibur/fc/node_modules/@midwayjs/orm/dist/configuration.js:25:36
    at IndexController.get [as user] (/Users/yuistack/Documents/calibur/fc/node_modules/@midwayjs/faas/node_modules/@midwayjs/core/dist/context/resolverHandler.js:69:36)
    at IndexController.printHelloWorld (/Users/yuistack/Documents/calibur/fc/packages/sign/src/index.ts:27:29)
    at FaaSStarter.invokeHandler (/Users/yuistack/Documents/calibur/fc/node_modules/@midwayjs/faas/src/starter.ts:205:36)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at /Users/yuistack/Documents/calibur/fc/node_modules/@midwayjs/faas/src/starter.ts:171:28 {
  name: 'RepositoryNotFoundError',
  message: 'No repository for "User" was found. Looks like this entity is not registered in current "default" connection?'
}

这是新版本的一个 bug 吗?还是说以后都不再支持这种从第三方包导入模型的方式了?

czy88840616 commented 3 years ago

依旧是支持的,2.6.x 应该没有对这块做修改。另外我看你的扩展(组件)的用法是比较老的方式,可能随时不兼容,尽量换成新的写法。https://www.yuque.com/midwayjs/midway_v2/component_development

czy88840616 commented 3 years ago

如有问题,可以继续提供简单的可复现 demo。

gogogo1024 commented 3 years ago

类似的问题有时候开始会出现,昨天和今天出现了两次,今天出现的一次,我重新用npm run dev竟然好了,所以感觉是ioc容器和数据库连接时机问题。

czy88840616 commented 3 years ago

如果必现的话,可以提供demo..

gogogo1024 commented 3 years ago

如果必现的话,可以提供demo..

偶现,还在排查中,怀疑是midwayjs中一些npm包不一致的问题。没有锁版本了,升级到最新版本突然好了在测试服务器机器上面。

gogogo1024 commented 3 years ago

如果必现的话,可以提供demo..

偶现,还在排查中,怀疑是midwayjs中一些npm包不一致的问题。没有锁版本了,升级到最新版本突然好了在测试服务器机器上面。

好像找到问题了,我有分布式的任务项目启动就会去使用orm的实体,大概的代码是这样 import { Provide, Inject, Task } from '@midwayjs/decorator'; import { ProjectService } from './project';

@Provide() export class DistributedTaskBuildStatusService { @Inject() projectService: ProjectService;

// 例如下面是每分钟执行一次,并且是分布式任务 @Task({ repeat: { cron: '/10 ' }, // 每十分钟一次 // * // ┬ ┬ ┬ ┬ ┬ ┬ // │ │ │ │ │ | // │ │ │ │ │ └ day of week (0 - 7) (0 or 7 is Sun) // │ │ │ │ └───── month (1 - 12) // │ │ │ └────────── day of month (1 - 31) // │ │ └─────────────── hour (0 - 23) // │ └──────────────────── minute (0 - 59) // └───────────────────────── second (0 - 59, optional) }) async processStatus() { // StageMean.STAGE_BUILD_BUILD; await this.projectService.processProjectBuildStatus(); console.log('processProjectBuildStatus'); } }