I have FormData object sent from react app. It contains email and audio data with multipart/form-data header, i want to check the req.body in the first middleware checkUser before passing it to multer middleware audioUpload (before uploading the audio).
FormData
const handleSendAudio = async () => {
let fd = new FormData();
fd.append("email", email);
fd.append("audio", audioData);
try {
const resp = await authFetch.post("/complaints/audio", fd, {
headers: { "Content-Type": "multipart/form-data" },
});
} catch (ex) {
console.log(ex);
}
};
API side: file: checkUser.js
module.exports = async function (req, res, next) {
const user = await User.findOne({ email: req.body.email });
if (!user)
return res.status(401).json({ error: "cannot perform such operation now" });
next();
};
file: route.js
router.post(
"/audio",
checkUser, // i want to check the body here before passing it to multer middleware but the req.body is empty here.
audioUpload.single("audio"), //multer middleware
async (req, res) => {
const audioPath = req.file.path;
const { email } = req.body;
console.log("audioPath: ", audioPath);
const complaint = new Complaint({
email,
audioUrl: audioPath,
});
await complaint.save();
res.status(200).json(complaint);
}
);