moleculerjs / moleculer-web

:earth_africa: Official API Gateway service for Moleculer framework
http://moleculer.services/docs/moleculer-web.html
MIT License
292 stars 118 forks source link

Could not handle the request when the request come with files and data in multipart #316

Open Alab2022 opened 1 year ago

Alab2022 commented 1 year ago

When the request come with files and data the handler(ctx) function call twice but the response comes one time how we handle this. @icebob My request payload is like below, Payload : { files[] : [file1,file2] , name : "testing" }

My Functions is

async handler(ctx) {
    console.log("ctx", "iam called", ctx.meta)
    return new this.Promise((resolve, reject) => {
        //reject(new Error("Disk out of space"));
        if (ctx.meta.filename) {
            const filePath = path.join(uploadDir, ctx.meta.filename || this.randomName());
            const f = fs.createWriteStream(filePath);
            f.on("close", () => {
                // File written successfully
                // console.log(`Uploaded file stored in '${filePath}'`);
                resolve({ filePath, meta: ctx.meta });
            });

            ctx.params.on("error", err => {
                console.log("File error received", err.message);
                reject(err);

                // Destroy the local file
                f.destroy(err);
            });

            f.on("error", () => {
                // Remove the errored file.
                fs.unlinkSync(filePath);
            });

            ctx.params.pipe(f);
        } else {
            resolve({ meta: ctx.meta });
        }
    });
},
icebob commented 1 year ago

Please create a repro example.

arthurgermano commented 9 months ago

I am having the same issue. It is calling the handler for each file uploaded.

So If I upload 2 files, it calls twice. If I upload 3 files, it calls three times.

Just tried to send more files using the handler notation for an action that specs upload files - connected via nats to the central API.

` const fastestValidatorParams = { $$strict: false, };

hooks: { before, after, }, params: fastestValidatorParams, rest: { type: "multipart", method: "POST", path: "myUpload", }, handler, }, `

It should not be hard to reproduce.