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

Uploading File along with metadata in moleculer framework #307

Open
vishalgilbilemoodys opened this issue Apr 7, 2022 · 1 comment
Open

Comments

@vishalgilbilemoodys
Copy link

vishalgilbilemoodys commented Apr 7, 2022

Hello,
I tried following the moleculer framework documentation for uploading the file as multipart, by making change in ApiGateway route & setting the aliases with the rest route as mutlipart:service.action. Below is the snap of the ApiGateway settings

 {
                    path: "/api2",
                    authorization: true, //run method `authorize` before calling handler
                    // Use bodyparser module
                    bodyParsers: {
                        json: false,
                        urlencoded: false
                    },
                    mappingPolicy: "all",
                    aliases: {
                        // File upload from HTML multipart form
                        "POST /v2.dataviewer.upload": "multipart:v2.dataviewer.upload"
                    },
                    // Route level busboy config.
                    // More info: https://github.com/mscdex/busboy#busboy-methods
                    busboyConfig: {
                        limits: { files: 1 }
                        // Can be defined limit event handlers
                        // `onPartsLimit`, `onFilesLimit` or `onFieldsLimit`
                    },
                    // Call before `broker.call`
                    onBeforeCall(ctx, route, req) {
                        this.addExtraMetaData_beforeCall(ctx, route, req);
                    },
                    // Call after `broker.call` and before send back the response
                    onAfterCall(ctx, route, req, res, data) {
                        this.addExtraMetaData_afterCall(
                            ctx,
                            route,
                            req,
                            res,
                            data
                        );

                        //do stuff
                        let urlSplit = req.url.split('/'),
                            services = '/' + urlSplit[1] + '/' + urlSplit[2];
                        // Increment this metric (api.usage.total) counter by 1 everytime a particular api is called.
                        // This metric is used in grafana to see how many times this api is called by a individual user
                        this.broker.metrics.increment("api.usage.total", {
                            endpoint: req.url,
                            services: services,
                            login: ctx.meta.user.login,
                            version: this.version
                        }, 1);
                        //!important, has to return data after doing stuff
                        return data;
                    },
                    onError(req, res, err) {
                        // Increment this metric (api.error.total) counter by 1 everytime there is an error in API call.
                        // This metric is used in grafana to see how many times API failed for user level
                        this.formatAndSendError(res, err);
                        this.broker.metrics.increment("api.error.total", {
                            endpoint: req.url,
                            login: req.$ctx.meta.user ? req.$ctx.meta.user.login : "@unauthorized",
                            version: this.version
                        }, 1);
                    }
                }

But still in the service layer I'm getting undefined in the service Action method

@Action({
		cache: false,
		params: {
		}
	})
	async upload(ctx) {
		let request = ctx.params;
		request.user = ctx.meta.user;
		console.log('CTX', ctx);
		console.log('Params', ctx.params);
		console.log('ctx.meta.$multipart', ctx.meta.$multipart); //undefined
		console.log("Received upload $params:", ctx.meta.$params); //undefined
		return null;
	}

Any help is highly appreciated.

@dolly1997
Copy link

Hi @vishalgilbilemoodys is this issue resolved? i am facing the same issue!

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

2 participants