Giter Club home page Giter Club logo

Comments (4)

icebob avatar icebob commented on July 24, 2024

please show the errors as well.

from moleculer-channels.

ujwal-setlur avatar ujwal-setlur commented on July 24, 2024

@icebob here is the code that generates the error:

const { ServiceBroker } = require('moleculer');
const ChannelMiddleware = require('@moleculer/channels').Middleware;

const main = async () => {
  // service schema
  const serviceSchema = {
    name: 'helper',

    channels: {
      async 'helper.sum'(payload) {
        // Calls the sum method
        return this.sum(payload.a, payload.b);
      },
    },

    methods: {
      sum(a, b) {
        return a + b;
      },
    },
  };

  // our channel middleware
  const mw = ChannelMiddleware({
    adapter: {
      type: 'Fake',
    },
  });

  // create broker
  let broker = new ServiceBroker({
    logger: {
      type: 'Console',
    },
    // middlewares: [mw], // If I add middleware here, it works!
  });

  broker.middlewares.add(mw); // If I add middleware here, it does not work!

  broker.createService(serviceSchema);

  await broker.start();

  await broker.stop();
};

main();

Here is the error:

node index.js
[2023-01-31T19:04:19.622Z] INFO  triloka-81500/BROKER: Moleculer v0.14.28 is starting...
[2023-01-31T19:04:19.623Z] INFO  triloka-81500/BROKER: Namespace: <not defined>
[2023-01-31T19:04:19.623Z] INFO  triloka-81500/BROKER: Node ID: triloka-81500
[2023-01-31T19:04:19.623Z] INFO  triloka-81500/REGISTRY: Strategy: RoundRobinStrategy
[2023-01-31T19:04:19.623Z] INFO  triloka-81500/REGISTRY: Discoverer: LocalDiscoverer
[2023-01-31T19:04:19.624Z] INFO  triloka-81500/BROKER: Serializer: JSONSerializer
[2023-01-31T19:04:19.626Z] INFO  triloka-81500/BROKER: Validator: FastestValidator
[2023-01-31T19:04:19.627Z] INFO  triloka-81500/BROKER: Registered 13 middleware(s).
[2023-01-31T19:04:19.633Z] INFO  triloka-81500/REGISTRY: '$node' service is registered.
[2023-01-31T19:04:19.634Z] INFO  triloka-81500/REGISTRY: 'helper' service is registered.
[2023-01-31T19:04:19.634Z] INFO  triloka-81500/$NODE: Service '$node' started.
[2023-01-31T19:04:19.634Z] INFO  triloka-81500/HELPER: Service 'helper' started.
/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/@moleculer/channels/src/index.js:210
                                await broker.Promise.mapSeries(
                                             ^

TypeError: Cannot read properties of undefined (reading 'Promise')
    at ServiceBroker.serviceCreated (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/@moleculer/channels/src/index.js:210:18)
    at /Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/middleware.js:111:29
    at Array.map (<anonymous>)
    at MiddlewareHandler.callSyncHandlers (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/middleware.js:111:16)
    at ServiceBroker.callMiddlewareHookSync (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/service-broker.js:679:27)
    at Service._init (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/service.js:271:15)
    at Service.parseServiceSchema (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/service.js:239:8)
    at new Service (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/service.js:64:20)
    at ServiceBroker.createService (/Users/ujwal/Projects/moleculer-channels-issue-60/node_modules/moleculer/src/service-broker.js:834:14)
    at main (/Users/ujwal/Projects/moleculer-channels-issue-60/index.js:50:10)

Node.js v18.13.0

from moleculer-channels.

icebob avatar icebob commented on July 24, 2024

Yeah this form won't work. The middleware uses the created hook to get the broker instance and initialize the middleware, but you add the middleware after the broker created so this hook is not called.
It's a little hacky, but you can try to call mw.created(broker); before adding.

from moleculer-channels.

ujwal-setlur avatar ujwal-setlur commented on July 24, 2024

Ok, no worries. I put it in the configuration

from moleculer-channels.

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.