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

Inject未赋值identifier时,特殊情况下,对应的namespace会错误,导致找不到该属性。 #1277

Closed
wants to merge 2 commits into from

Conversation

roy2an
Copy link

@roy2an roy2an commented Sep 7, 2021

以引用 @midwayjs/axios 为例

import { Provide, Inject, Logger } from '@midwayjs/decorator';
import { HttpService } from '@midwayjs/axios';

@Provide()
export class GetAccessTokenService {
  @Inject()
  httpService: HttpService;

  @Inject('cache:cacheManager')
  cache;

  async execute(args, scope, next): Promise<any> {
    const { data, status } = await this.httpService.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=${appid}&secret=${secret}`);
  }
}
@Configuration({
  imports: [
    '@midwayjs/axios',
  ],
  namespace: 'WX',
  importConfigs: [
    join(__dirname, 'config')
  ]
})
export class WechatConfiguration implements ILifeCycle {
  @App()
  app: IMidwayApplication

  @Inject()
  appDir: any

  @Logger()
  coreLogger: ILogger;

  async onReady(content: IMidwayContainer) {
  }
}

如果将整个component发布成包的形式进行调用,系统会报错。报错如下:

NotFoundError: WX:httpService in class GetAccessTokenService is not valid in current context

httpService的namespace变成了WX。

Checklist
  • npm test passes
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message follows commit guidelines
Affected core subsystem(s)

@midwayjs/core
@midwayjs/decorator

Description of change

在savePropertyInject函数中,为metadata中加入新的属性originDesign,存放inject属性对应的类型。并在restoreDefinition函数中根据originDesign值查询正确的identifier。

roy2an and others added 2 commits September 2, 2021 12:33
将`midway`生成的`schedule`注册进`egg`的`schedule`中。确保`schedule`可以被找到,避免只能通过`agent.schedule.closed`的方式才能关停`schedule`,而导致所有`schedule`都关闭。
@gitpod-io
Copy link

gitpod-io bot commented Sep 7, 2021

@czy88840616
Copy link
Member

czy88840616 commented Sep 7, 2021

这个最新的那个 mr 可能已经修了。

@czy88840616
Copy link
Member

另外,不要再使用字符串引入组件。

@czy88840616
Copy link
Member

试试新版本是否还有问题。

@roy2an
Copy link
Author

roy2an commented Sep 7, 2021

新版本可以了

@roy2an roy2an closed this Sep 7, 2021
@waitingsong
Copy link
Member

另外,不要再使用字符串引入组件。

那么

  @Inject('cache:cacheManager')
  cache;

这个的字符串参数还能用么?

@czy88840616
Copy link
Member

czy88840616 commented Sep 7, 2021

当前版本还可以,2.x 里可以继续用,3.x 还在考虑,作为一个兼容的话应该也可以,不过尽可能推进使用 class 作为类型,这样定义也有了。

@waitingsong
Copy link
Member

当前版本还可以,2.x 里可以继续用,3.x 还在考虑,作为一个兼容的话应该也可以,不过尽可能推进使用 class 作为类型,这样定义也有了。

那以后组件中的 namespace 的价值就不大了吧

@czy88840616
Copy link
Member

对,虽然逻辑里会慢慢弱化 namespace 但是还是会作为排错的字段信息存在

@roy2an
Copy link
Author

roy2an commented Sep 7, 2021

试试新版本是否还有问题。

又发现了一个新的问题。还是在发布包的情况下。

import {
  Inject,
  Controller,
  Provide,
  Get
} from "@midwayjs/decorator";

import { DemoService } from "../service/demo";

@Provide()
@Controller('/')
export class DemoController {
  @Inject()
  ctx;

  @Inject() // 这样注入,demoService中的ctx为undefined
  demoService: DemoService; 

  @Inject('DEMO:demoService')// 这样注入,demoService中的ctx才有值
  demoService; 

  @Get('/test')
  async test () {
    await this.demoService.execute()
  }
}

import { Provide, Inject } from '@midwayjs/decorator';
import { Context } from 'egg';

@Provide()
export class DemoService {
  @Inject()
  ctx: Context

  async execute(): Promise<any> {
    console.log(this.ctx)
  }
}

@czy88840616
Copy link
Member

像是是在组件的情况下请求作用域对象没找到的样子

@czy88840616
Copy link
Member

czy88840616 commented Sep 8, 2021

@roy2an 我模拟不出来,有没有具体的 repo case?

@roy2an
Copy link
Author

roy2an commented Sep 8, 2021

@roy2an 我模拟不出来,有没有具体的 repo case?

我一会儿发个repo

@roy2an
Copy link
Author

roy2an commented Sep 8, 2021

@roy2an 我模拟不出来,有没有具体的 repo case?

https://github.com/roy2an/midwayjs-ctx.git

@czy88840616
Copy link
Member

已定位,开始修复

@czy88840616
Copy link
Member

@roy2an 试试2.13.1

@roy2an
Copy link
Author

roy2an commented Sep 8, 2021

@roy2an 试试2.13.1

好的

@roy2an
Copy link
Author

roy2an commented Sep 8, 2021

@roy2an 试试2.13.1

好的

貌似npm各节点还没有更新。晚点再看看。

@czy88840616
Copy link
Member

我的错。。。现在好了

@roy2an
Copy link
Author

roy2an commented Sep 8, 2021

我的错。。。现在好了

测试了。已OK。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants