Moleculer: ctx.meta is empty in events

Created on 1 Apr 2021  路  1Comment  路  Source: moleculerjs/moleculer

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm reporting the issue to the correct repository

Current Behavior

I am calling an action, and the action has ctx.meta available. as I set it during authorize phase.
In hooks -> after, I change some response based on condition and emit an event.
Emitting event has all data but missing meta. When I access ctx.meta, it is empty.

Expected Behavior

It should have ctx.meta available.

Failure Information

Steps to Reproduce

  1. Call action
  2. in hooks, after phase, emit an event, this.broker.emit('user..created', data, ['test']);

  3. try to access, ctx.meta, It is empty

Reproduce code snippet

https://codesandbox.io/s/moleculer-sample-forked-wji7t?file=/index.js

const broker = new ServiceBroker({
    logger: console,
    transporter: "NATS"
});

broker.createService({
    name: "test",
    events: {
        "user.created": {
            async handler(ctx) {
                console.log("Hello Meta: ", ctx.meta);
            }
        }
    },
    actions: {
        create(ctx) {
            ctx.meta.isAllowed = true;
            const newUser = {
                id: 1,
                name: 'any',
                isAllowed: ctx.meta.isAllowed
            }
            return newUser;
        }
    },
    hooks: {

        after: {
            async create(ctx, res) {
                ctx.broker.emit('user.created', res, ['test']);
                return res;
            }
        }
    }
});

Context

  • Moleculer version: 0.14.12
  • NodeJS version: 12.18.0
  • Operating System: ubuntu
Question

Most helpful comment

>All comments

Was this page helpful?
0 / 5 - 0 ratings

Related issues

HighSoftWare96 picture HighSoftWare96  路  4Comments

molobala picture molobala  路  3Comments

kesslerdev picture kesslerdev  路  4Comments

maitrucquynhq111 picture maitrucquynhq111  路  3Comments

DeividasJackus picture DeividasJackus  路  4Comments