GithubHelp home page GithubHelp logo

Comments (6)

shiny avatar shiny commented on May 17, 2024

相关代码
image

from egg-bus.

shiny avatar shiny commented on May 17, 2024

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

from egg-bus.

shiny avatar shiny commented on May 17, 2024

今天继续调试,现在是在 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 会消失?

from egg-bus.

seekcx avatar seekcx commented on May 17, 2024

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

from egg-bus.

seekcx avatar seekcx commented on May 17, 2024

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

from egg-bus.

superlbr avatar superlbr commented on May 17, 2024

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

from egg-bus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.