Как сказано в названии. Я отправляю данные многостраничной формы от почтальона с несколькими текстовыми полями, а также изображением. Однако, когда я console.log (req.body), я получаю
{
'------WebKitFormBoundaryCpNHg1P01X54QAVr\r\nContent-Disposition: form-data; name': '"customerID"\r\n' +
'\r\n' +
'1\r\n' +
'------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
'Content-Disposition: form-data; name="make"\r\n' +
'\r\n' +
'honda\r\n' +
'------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
'Content-Disposition: form-data; name="model"\r\n' +
'\r\n' +
'accord\r\n' +
'------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
'Content-Disposition: form-data; name="year"\r\n' +
'\r\n' +
'2020\r\n' +
'------WebKitFormBoundaryCpNHg1P01X54QAVr\r\n' +
'Content-Disposition: form-data; name="color"\r\n' +
'\r\n' +
'white\r\n' +
'------WebKitFormBoundaryCpNHg1P01X54QAVr--\r\n'
}
Если я console.log (req.body.keyName), у меня всегда есть undefined. Я уже реализовал то, что предлагалось в похожих сообщениях, т.е. используя
app.use(bodyParser.urlencoded({
extended: true
}));
, но это все еще не работает. Я не уверен, что делаю не так. Далее следуют фрагменты кода.
сервер. js
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cors());
app.use('/users', userRouter);
app.use('/offers', offerRouter);
app.listen(PORT, (err)=>
{
if (err) console.log(err);
else console.log(`Server listening on port ${PORT}`);
});
маршрут
const multer = require('multer');
//required for mulitpart-formdata
const diskStorage = multer.diskStorage({
destination: "uploads/",
filename: (req, file, call_back) => {
//Prepend date to the filename or anything that makes
//the file unique so it won't be overwritten
call_back(null, Date.now() + "_" + file.originalname);
},
});
const upload = multer({ storage: diskStorage });
router.post("/request", upload.single("file"), (req, res, next) =>
{
GetBucket();
console.log(req.file);
})
почтальон 