如何从请求中访问非文件字段中的文本?(我正在使用不育症)
我们的请求使用Insomia

我们可以通过循环遍历部件来访问文件字段。使用const parts = await req.files();。
index.js
import Fastify from "fastify";
import FastifyMultipart from "fastify-multipart";
export const fastify = await Fastify();
fastify.register(FastifyMultipart);controllers/property.js
export const addProperty = async function (req, res) {
try {
// WE ACCESS FILES OF MULTIPART FORM REQUEST HERE
const parts = await req.files();
for await (const data of parts) {
console.log("*******File being access**********");
console.log(data.filename); // access file name
...
}
// HOW DO WE ACCESS OTHER *NON_FILES* FIELDS?
...
res.status(201).send({ message: "Property Added!." });
} catch (error) {
res.send(error);
}
};在控制器脚本中,我们使用await req.files();访问文件。
如何访问非文件字段(如文本)?
发布于 2022-04-18 13:50:15
有两种方法可以获取docs https://github.com/fastify/fastify-multipart中给出的其他数据

现在您可以访问data.fields中的名称了
const data = await req.file();
console.log(data.fields.name.value); // virenderfastify.register(require('fastify-multipart'), { attachFieldsToBody: true });
const file = req.body.image1;
const name = req.body.name.valuehttps://stackoverflow.com/questions/70477141
复制相似问题