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

有时无法访问到 this.app.mysql #6

Closed
shiny opened this issue Sep 20, 2019 · 6 comments · Fixed by #7
Closed

有时无法访问到 this.app.mysql #6

shiny opened this issue Sep 20, 2019 · 6 comments · Fixed by #7
Assignees
Labels
question Further information is requested

Comments

@shiny
Copy link

shiny commented Sep 20, 2019

在 Listener 中添加方法访问 this.app.mysql,观察到诡异的情况:有几次能正常访问,有时则提示
TypeError: Cannot read property 'queryOne' of undefined

类似下图,前两次正常执行,最后一次突然出现错误提示。
image

@shiny
Copy link
Author

shiny commented Sep 20, 2019

相关代码
image

@shiny
Copy link
Author

shiny commented Sep 20, 2019

此外,有时是 this.ctx.service 中的对象无法访问到,我不得不用 this.app.createAnonymousContext(); 单独创建 ctx 来保证能稳定访问到 service

@Runrioter Runrioter added the question Further information is requested label Sep 21, 2019
@shiny
Copy link
Author

shiny commented Sep 21, 2019

今天继续调试,现在是在 listener 的 run 里
无论是 const ctx = this.app.createAnonymousContext(); 访问 ctx,还是 this.ctx

  1. 调试时偶尔访问不到 ctx.service,再次 emit 触发后又能访问到
  2. 重启 egg 进程后亦有同样的问题

打印 ctx 发现,无法访问时是

egg_1     |  { request:egg_1     |    { method: 'GET',
egg_1     |      url: '/',
egg_1     |      header: { host: '127.0.0.1', 'x-forwarded-for': '127.0.0.1' } },
egg_1     |   response:
egg_1     |    { status: 200,
egg_1     |      message: 'OK',
egg_1     |      header: [Object: null prototype] {} },
egg_1     |   app:
egg_1     |    { env: 'local',
egg_1     |      name: 'src',
egg_1     |      baseDir: '/usr/src/app',
egg_1     |      subdomainOffset: 2,
egg_1     |      config: '<egg config>',
egg_1     |      httpclient: '<egg httpclient>',
egg_1     |      loggers: '<egg loggers>',
egg_1     |      router: '<egg router>' },
egg_1     |   originalUrl: '/',
egg_1     |   req: '<original node req>',
egg_1     |   res: '<original node res>',
egg_1     |   socket: '<original node socket>' }

可以访问时是

egg_1     |  { request:
egg_1     |    { method: 'GET',
egg_1     |      url: '/',
egg_1     |      header: { host: '127.0.0.1', 'x-forwarded-for': '127.0.0.1' } },
egg_1     |   response:
egg_1     |    { status: 200,
egg_1     |      message: 'OK',
egg_1     |      header: [Object: null prototype] {} },
egg_1     |   app:
egg_1     |    { env: 'local',
egg_1     |      name: 'src',
egg_1     |      baseDir: '/usr/src/app',
egg_1     |      subdomainOffset: 2,
egg_1     |      config: '<egg config>',
egg_1     |      controller: '<egg controller>',
egg_1     |      httpclient: '<egg httpclient>',
egg_1     |      loggers: '<egg loggers>',
egg_1     |      middlewares: '<egg middlewares>',
egg_1     |      router: '<egg router>',
egg_1     |      serviceClasses: '<egg serviceClasses>' },
egg_1     |   originalUrl: '/',
egg_1     |   req: '<original node req>',
egg_1     |   res: '<original node res>',
egg_1     |   socket: '<original node socket>' }

区别是前者没有 ctx.app.serviceClasses,后者有。不清楚为什么 ctx.app.serviceClasses 会消失?

@seekcx
Copy link
Collaborator

seekcx commented Sep 22, 2019

初步判断可能是和 worker 有关,稍后确认一下,感谢反馈。

@seekcx
Copy link
Collaborator

seekcx commented Sep 23, 2019

app.mysql 无法访问

mysql 配置中将 agent 设置为 true,默认是 false

config.mysql = {
  client: {
    host: 'localhost',
    port: '3306',
    user: 'root',
    password: 'secret',
    database: 'test',
  },
  app: true,
  agent: true,  // <-- 置为 true
};

service 无法访问

这是由 agent 引起的,在项目目录下创建一个 agent.js 就可以复现这个问题:

'use strict';

module.exports = app => {
  const ctx = app.createAnonymousContext();
  console.log(ctx.service); // undefined
};

目前无法确定是写法不对还是 egg 的 bug,已经提交相关 issue 至 egg(eggjs/egg#3946),等待反馈结果。

可以先自己手动使用 app.createAnonymousContext() 创建 ctx

@superlbr
Copy link
Contributor

有时无法访问到ctx.service。。。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants