moleculerjs / moleculer-http-client

HTTP client mixin that allows Moleculer services to communicate with remote REST APIs
MIT License
18 stars 7 forks source link

Error in console after adding moleculer-http-client as mixins #5

Closed yogesh-rs closed 3 years ago

yogesh-rs commented 4 years ago
  1. require const HTTPClientService = require("moleculer-http-client");
  2. mixins: [HTTPClientService], // this line throws an error in console
  3. Run npm run dev

--

Result :

Request error! TypeError : Converting circular structure to JSON
    --> starting at object with constructor 'ServiceBroker'
    |     property 'services' -> object with constructor 'Array'
    |     index 0 -> object with constructor 'Service'
    --- property 'broker' closes the circle
 TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'ServiceBroker'
    |     property 'services' -> object with constructor 'Array'
    |     index 0 -> object with constructor 'Service'
    --- property 'broker' closes the circle
    at JSON.stringify (<anonymous>)
    at Service.encodeResponse (/Volumes/Boskysoft/work-hdd/recrutiment-smart/play/nodejs/moleculer-js/http-client/node_modules/moleculer-web/src/index.js:649:16)
    at Service.sendResponse (/Volumes/Boskysoft/work-hdd/recrutiment-smart/play/nodejs/moleculer-js/http-client/node_modules/moleculer-web/src/index.js:749:18)
    at Service.callAction (/Volumes/Boskysoft/work-hdd/recrutiment-smart/play/nodejs/moleculer-js/http-client/node_modules/moleculer-web/src/index.js:625:10)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async /Volumes/Boskysoft/work-hdd/recrutiment-smart/play/nodejs/moleculer-js/http-client/node_modules/moleculer-web/src/index.js:460:21

/API: <= 500 GET /api/~node/services?withActions=true [+15.368 ms]

AndreMaz commented 4 years ago

Can you please add the actual code to reproduce the issue?

arun-prakash-fokus commented 3 years ago

Steps to reproduce this issue:

  1. Update your greetings.service.js as shown below
  2. Run npm run dev
  3. Check your services
  4. Open your browser and navigate to http://localhost:3000/

So far no issues

Navigate to Services tab image

There are no services listed and in the console, you get the error messages described in the issue reported by @yogesh-rs

// greetings.service.js
"use strict";

/**
 * @typedef {import('moleculer').Context} Context Moleculer's Context
 */

// https://github.com/moleculerjs/moleculer-http-client
const HTTPClientService = require('moleculer-http-client');

module.exports = {
    name: "greeter",

    mixins: [HTTPClientService],

    /**
     * Settings
     */
    settings: {

    },

    /**
     * Dependencies
     */
    dependencies: [],

    /**
     * Actions
     */
    actions: {

        /**
         * Say a 'Hello' action.
         *
         * @returns
         */
        hello: {
            rest: {
                method: "GET",
                path: "/hello"
            },
            async handler() {
                return this.hello();
            }
        },

        /**
         * Welcome, a username
         *
         * @param {String} name - User name
         */
        welcome: {
            rest: "/welcome",
            async handler() {
                return this.welcome();
            }
        }
    },

    /**
     * Events
     */
    events: {

    },

    /**
     * Methods
     */
    methods: {
        async hello() {
            this.broker.call("greeter.get", {
                url: "https://httpbin.org/json",
                opt: { responseType: "json" }
            })
                .then(res => this.broker.logger.info(res))
                .catch(error => this.broker.logger.error(error));
        },

        async welcome() {
            this._get("https://httpbin.org/json", { responseType: "json" })
                .then(res => this.broker.logger.info(res))
                .catch(error => this.broker.logger.error(error));
        },
    },

    /**
     * Service created lifecycle event handler
     */
    created() {

    },

    /**
     * Service started lifecycle event handler
     */
    async started() {

    },

    /**
     * Service stopped lifecycle event handler
     */
    async stopped() {

    }
};
AndreMaz commented 3 years ago

Thanks @arun-prakash-fokus I've fixed the issue and released it in v0.4.1

arun-prakash-fokus commented 3 years ago

Thank you for the quick fix.

On another note, we have the actions from the HTTPClientService shown through the API gateway for the greeter service. Is this the expected behavior?

image

may i suggest adding

visibility: "private",

to the HTTP actions provided by the HttpClientService. This way, when mixed in, they are still available to the local service, but not visible through the gateway.

Thoughts & comments

AndreMaz commented 3 years ago

Yes, by default all actions are public and visible. However, you can mark them as private or even remove them, if necessary, by adapting the method filtering example