英文:
Moleculer Framework API code is not able to use the file from request body (form-data). returns undefined
问题
I am new to the Moleculer framework and need to upload a file from an API request body to MongoDB using an action handler. I have been able to upload files using Multer with Express, but I need to use only the Moleculer framework. When I run the action handler, the "req" parameter doesn't take the file from the request body and it's always undefined. I need help finding a way to make the file come to the action handler.
(https://i.stack.imgur.com/Xv7YP.png)
actions: {
create: {
rest: "POST /",
async handler(ctx) {
try {
console.log("Params are: " + ctx.params.file);
console.log("Meta is: " + ctx.meta.file);
console.log("File is: " + ctx.file);
} catch(err) {
console.log("Error: " + err);
}
}
},
},
英文:
I am new to the Moleculer framework and need to upload a file from an API request body to MongoDB using an action handler. I have been able to upload files using Multer with Express, but I need to use only the Moleculer framework. When I run the action handler, the "req" parameter doesn't take the file from the request body and it's always undefined. I need help finding a way to make the file come to the action handler.
(https://i.stack.imgur.com/Xv7YP.png)
actions: {
create: {
rest: "POST /",
async handler(ctx) {
try {
console.log("Params are: " + ctx.params.file);
console.log("Meta is: " + ctx.meta.file);
console.log("File is: " + ctx.file);
} catch(err) {
console.log("Error: " + err);
}
}
},
},
答案1
得分: 0
文件上传在操作中作为流处理,但应在路由设置中进行配置。您可以在文档中找到相关信息:https://moleculer.services/docs/0.14/moleculer-web.html#File-upload-aliases
例如:
module.exports = {
mixins: [ApiGateway],
settings: {
path: "/upload",
routes: [
{
path: "",
aliases: {
// 从HTML多部分表单上传文件
"POST /": "multipart:file.save",
// 从AJAX或cURL上传文件
"PUT /:id": "stream:file.save",
// 从HTML表单上传文件并覆盖busboy配置
"POST /multi": {
type: "multipart",
// 操作级别的busboy配置
busboyConfig: {
limits: { files: 3 }
},
action: "file.save"
}
},
// 路由级别的busboy配置。
// 更多信息:https://github.com/mscdex/busboy#busboy-methods
busboyConfig: {
limits: { files: 1 }
// 可以定义限制事件处理程序
// `onPartsLimit`, `onFilesLimit`或`onFieldsLimit`
},
mappingPolicy: "restrict"
}
]
}
});
英文:
The file upload is handled as a Stream in actions, but you should configure it in the route settings. In the documentation you can find information about it: https://moleculer.services/docs/0.14/moleculer-web.html#File-upload-aliases
E.g:
module.exports = {
mixins: [ApiGateway],
settings: {
path: "/upload",
routes: [
{
path: "",
aliases: {
// File upload from HTML multipart form
"POST /": "multipart:file.save",
// File upload from AJAX or cURL
"PUT /:id": "stream:file.save",
// File upload from HTML form and overwrite busboy config
"POST /multi": {
type: "multipart",
// Action level busboy config
busboyConfig: {
limits: { files: 3 }
},
action: "file.save"
}
},
// 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`
},
mappingPolicy: "restrict"
}
]
}
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论