Javascript 如何在一个请求中发布FormData对象和附加对象?

Javascript 如何在一个请求中发布FormData对象和附加对象?,javascript,node.js,Javascript,Node.js,我有下一个代码(前端): 下一个后端部分: app.post('/upload', upload.array('fileLoader', 5), (req, res, next) => { const files = req.files; if (!files) { const error = new Error('Please upload a file'); error.httpStatusCode = 400; retu

我有下一个代码(前端):

下一个后端部分:

app.post('/upload', upload.array('fileLoader', 5), (req, res, next) => {
    const files = req.files;
    if (!files) {
        const error = new Error('Please upload a file');
        error.httpStatusCode = 400;
        return next(error);
    }
    res.send(files);
})

如何同时访问req.body中的对象数据和req.files中的图像?

您可以使用Formdata向
imagesData
添加一个新值

让imagesData=newformdata();
按钮。addEventListener('单击',()=>{
/*
*注意:set函数为现有键设置一个新值
*或者如果键/值不存在,则添加该键/值
*/
imagesData.set('id',5);
imagesData.set('name','five');
上传数据到服务器(imagesData);
})
const uploadDataToServer=(数据)=>{
轴心柱http://localhost:5000/upload",数据)
.然后((res)=>console.log(res))
.catch((err)=>console.log(err));

}
谢谢,我如何在后端访问此字段?您是否尝试过
req.id
req.name
?您是否尝试在服务器端打印或调试请求?是的,但我在console.log(req.id,req.name)中没有定义;好的,非常感谢,这个字段在req.body中。
app.post('/upload', upload.array('fileLoader', 5), (req, res, next) => {
    const files = req.files;
    if (!files) {
        const error = new Error('Please upload a file');
        error.httpStatusCode = 400;
        return next(error);
    }
    res.send(files);
})