moleculer-web
moleculer-web copied to clipboard
Could not handle the request when the request come with files and data in multipart
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 });
}
});
},
Please create a repro example.
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, }; ["myUpload"]: { hooks: { before, after, }, params: fastestValidatorParams, rest: { type: "multipart", method: "POST", path: "myUpload", }, handler, },
It should not be hard to reproduce.