如何从 NodeJS API 的角度处理额外的多个文件上传?
How to handle additional multer file uploads from a NodeJS API perspective?
我正在构建一个与 MongoDB 通信的 NodeJS API。我不知道如何通过 API 正确处理多个文件上传。我有一个 PATCH 路由,可以处理传递给它的任何 JSON 参数,并根据指定的 ID 更新文档。
补丁路线:
app.route('/api/Vehicles/:vehicleId')
.patch(upload.single('carFile'),clickHandler.updateId)
文件:
{
"carDoc": [
"http://localhost:3000/uploadsVehicle/2019-06-21T08:13:04.068Z_X5.jpg",
"http://localhost:3000/uploadsVehicle/2019-06-21T08:38:03.562Z_X5.jpg"
],
"_id": "5d0a039e16bf91332d968b46",
"carNum": "XXX325",
"carManager": "Gzegorz Tomasevic",
"insuranceExp": "2021-06-06T00:00:00.000Z",
"__v": 0
}
下面的函数可以很好地处理 PATCH 请求,但是由于我现在需要以某种方式将文件关联到我的 mongo 文档,我已经实现了如果文件被 req.file
访问,那么指定文档中的数组 carDoc
已使用 Web link 更新为新上传的 image/pdf/whatever。如您所见,下面的函数检查是否正在上传任何文件,执行 if 下的操作,然后继续对文档中的其他字段执行更新,因为我最初将其设计为仅处理 PATCH 请求。因此,如果我只想上传一个新文件并在 carDoc
数组中创建一个新的 link,我最终会收到 404 响应,因为 Vehicle.update
函数看不到我已经推送了一个new weblink 到 carDoc
数组中。有没有一种方法可以将这两个操作($push 和 $set)结合起来,这样我就可以收到一个可以正确处理的响应?谢谢!
请求处理程序:
// modify doc by id
this.updateId = function (req, res) {
const id = req.params.vehicleId;
const updateObject = req.body;
// if a file is uploaded add path to existing carDoc array
if (req.file) {
Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + req.file.path]}})
.exec()
.then()
}
// update and error handling
Vehicle.update({_id: id}, {$set: updateObject })
.exec()
.then(doc => {
console.log(doc);
if (doc.n === 0) {
res.status(404).json({message: "document you are trying to modify does not exist."})
}
else {
res.status(200).json({message: id +" document successfully modified.", changes: updateObject})
}
})
.catch(err => {
res.status(500).json({
error: err
})
});
};
参考multer link了解multer npm在Node.JS中的使用方法。
var upload = multer({ dest: 'dest_folder_path/' });
填充将存储在上面的目标位置,然后在您的代码更新文档路径中,然后将其保存在 mongodb。
更改代码中的以下行。
Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + dest_folder_path/foldername]}}
表示文件存放在代码服务器http://localhost:3000/dest_folder_path
找到了 Promise.all([])
的方法:
// check if file is being uploaded
var fileQuery = req.file ? Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + req.file.path]}}) : Promise.resolve();
// execute any other changes
var bodyQuery = Vehicle.update({_id: id}, {$set: updateObject })
// handle single response etc.
Promise.all([fileQuery, bodyQuery])
.then(result => {})
我正在构建一个与 MongoDB 通信的 NodeJS API。我不知道如何通过 API 正确处理多个文件上传。我有一个 PATCH 路由,可以处理传递给它的任何 JSON 参数,并根据指定的 ID 更新文档。
补丁路线:
app.route('/api/Vehicles/:vehicleId')
.patch(upload.single('carFile'),clickHandler.updateId)
文件:
{
"carDoc": [
"http://localhost:3000/uploadsVehicle/2019-06-21T08:13:04.068Z_X5.jpg",
"http://localhost:3000/uploadsVehicle/2019-06-21T08:38:03.562Z_X5.jpg"
],
"_id": "5d0a039e16bf91332d968b46",
"carNum": "XXX325",
"carManager": "Gzegorz Tomasevic",
"insuranceExp": "2021-06-06T00:00:00.000Z",
"__v": 0
}
下面的函数可以很好地处理 PATCH 请求,但是由于我现在需要以某种方式将文件关联到我的 mongo 文档,我已经实现了如果文件被 req.file
访问,那么指定文档中的数组 carDoc
已使用 Web link 更新为新上传的 image/pdf/whatever。如您所见,下面的函数检查是否正在上传任何文件,执行 if 下的操作,然后继续对文档中的其他字段执行更新,因为我最初将其设计为仅处理 PATCH 请求。因此,如果我只想上传一个新文件并在 carDoc
数组中创建一个新的 link,我最终会收到 404 响应,因为 Vehicle.update
函数看不到我已经推送了一个new weblink 到 carDoc
数组中。有没有一种方法可以将这两个操作($push 和 $set)结合起来,这样我就可以收到一个可以正确处理的响应?谢谢!
请求处理程序:
// modify doc by id
this.updateId = function (req, res) {
const id = req.params.vehicleId;
const updateObject = req.body;
// if a file is uploaded add path to existing carDoc array
if (req.file) {
Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + req.file.path]}})
.exec()
.then()
}
// update and error handling
Vehicle.update({_id: id}, {$set: updateObject })
.exec()
.then(doc => {
console.log(doc);
if (doc.n === 0) {
res.status(404).json({message: "document you are trying to modify does not exist."})
}
else {
res.status(200).json({message: id +" document successfully modified.", changes: updateObject})
}
})
.catch(err => {
res.status(500).json({
error: err
})
});
};
参考multer link了解multer npm在Node.JS中的使用方法。
var upload = multer({ dest: 'dest_folder_path/' });
填充将存储在上面的目标位置,然后在您的代码更新文档路径中,然后将其保存在 mongodb。
更改代码中的以下行。
Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + dest_folder_path/foldername]}}
表示文件存放在代码服务器http://localhost:3000/dest_folder_path
找到了 Promise.all([])
的方法:
// check if file is being uploaded
var fileQuery = req.file ? Vehicle.findByIdAndUpdate({_id: id}, {$push: {carDoc: ["http://localhost:3000/" + req.file.path]}}) : Promise.resolve();
// execute any other changes
var bodyQuery = Vehicle.update({_id: id}, {$set: updateObject })
// handle single response etc.
Promise.all([fileQuery, bodyQuery])
.then(result => {})