I rarely ask someone for help, but in this particular case I can't figure out what to do.
I would like to handle PUT requests to my API to update either "name" or "dllFile" or both values in my MongoDB database, but whatever I do, it returns "null" for a value I don't specify.
It's better for me to show my problem on my example, look at the code, please:
exports.dlls_update_one = (req, res, next) => {
const id = req.params.dllId
const name = req.body.name
const dllFile = req.file.path
DLL
.findOneAndUpdate(id, { $set: { name, dllFile }})
.exec()
.then(result => {
res.status(200).json({
message: 'File successfully updated!',
get_info: {
type: 'GET',
description: 'Get information about this DLL',
url: `https://xxx.xxxx.xx/api/dlls/${result._id}`
}
});
})
.catch(err => {
res.status(500).json({
error: err
});
});
}
So, my problem is that I would like to update a DLL file, which resides on my server and its JSON property ("dllFile") with its file path in a database. I also want to update the "name" property if I have to.
I send a file in a multipart form with multer as a middleware - it works, but I also store its path in a "dllFile" property which I require to be changed when file is uploaded. I have the name and dllFile properties in findOneAndUpdate() which are to be set.
The thing is, when I set two values - it works, the properties are changed properly, but when I for example send only the file, its path is changed in dllFile, but name is set to "null". I know why it sets to null, but I don't know what to do if I want to set only one value.
I came up with a for loop which loops through properties in a database, but it works only for name in JSON, but it doesn't work in multipart form.
Sample response I get from updated file:
{
"dll": {
"_id": "5bde21f068a6d80712e67fab",
"name": null,
"dllFile": "uploads\\1541541269220msdia80.dll"
}
}
I appreciate any help from anyone. I hope you understand what I mean. If you have any question, please ask. I will do whatever it takes to get this work.