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

subscriber's event function ctx with different structure #1070

Closed
lling1105 opened this issue Mar 24, 2022 · 5 comments
Closed

subscriber's event function ctx with different structure #1070

lling1105 opened this issue Mar 24, 2022 · 5 comments

Comments

@lling1105
Copy link

i have 2 clients subscribe the same event, when both receive the notice, one has ctx and can get the params correctlly, but the other one ctx is the params value, no params path, why?

both client service with the same codes,
#1 client event receiver:
ctx.params -> {"result":"ok"}

#2 client event receiver:
ctx -> {"result":"ok"} and no ctx.params

@AndreMaz
Copy link
Member

Hi @lling1105 can you create a repro repo please?

@lling1105
Copy link
Author

@AndreMaz this issue appeared when use webpack.js to build bundle.js, guess it's about the output setting: libraryTarget: 'commonjs2'. i changed to other kind target, but still the same

@lling1105
Copy link
Author

solved! set webpack optimization.minimizer to none

@ngraef
Copy link
Contributor

ngraef commented Mar 25, 2022

This seems like it's related to moleculer's support for two different function signatures for event handlers (see the 0.14 changelog). The code that determines which signature to use relies on the name of the parameter being ctx or context:

moleculer/src/service.js

Lines 32 to 34 in 422876c

function isNewSignature(args) {
return args.length > 0 && ["ctx", "context"].indexOf(args[0].toLowerCase()) !== -1;
}

Webpack (or any other) minimizer will usually change the parameter name, causing the function to be passed params for the old event handler signature. As the changelog notes, you can add context: true to the event definition to force the new signature if you want to continue using a minimizer.

@icebob
Copy link
Member

icebob commented Mar 25, 2022

Exactly.

@icebob icebob closed this as completed Apr 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants