【发布时间】:2021-03-07 15:56:55
【问题描述】:
我正在为快速 js 中的文件上传而苦苦挣扎。 我有这个猫鼬计划:
{
title: {
type: String,
required: true,
min: 1,
max: 1024,
},
whatToRead: [{ type: String }],
questions: [
{
question: {
type: String,
required: true,
},
options: [
{
option: String,
correct: {
type: Boolean,
default: false,
},
},
],
},
],
date: {
type: Date,
default: Date.now,
},
我可以使用以下代码轻松地将新项目添加到数据库中:
const { title, whatToRead, questions} = req.body;
const quiz = new Quiz({
title,
whatToRead,
questions,
});
try {
await quiz.save();
res.send({ msg: "saved", id: quiz.id });
} catch (err) {
res.status(400).send(err.message);
}
但是现在我需要为项目添加图像。我尝试使用 multer 进行此操作,并且成功了,但是我无法发送包含所有其他信息的 json 文件,因为它必须是 multipart/form-data,而不是 json。如何同时发送 json 和文件?
【问题讨论】:
标签: javascript node.js express multer