0

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.

Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
szakes1
  • 794
  • 3
  • 16
  • 35
  • `{ new: true }` is missing from the options. That means the "previous state" is returned without it. – Neil Lunn Nov 06 '18 at 22:27
  • Hey! It works perfectly. I am surprised it was that easy to do. Thank you, man! You could've added an answer instead of a comment, so I would "accept" your answer. – szakes1 Nov 06 '18 at 22:35
  • What I "did" do is link to the "existing answer", which you "could" have searched for and found the result. Which is what https://www.google.com/search?q=mongoose+findoneandupdate+returns+null would have actually pointed to as the top result. More than likely it was also "suggested" when you created the post, and the UI does ask you to check that. Also we don't need you "whole stack" as tags on a question. Just the tags that relate directly to the question being asked. i.e `multer` has been removed by me since nothing in the question relates to `multer`. – Neil Lunn Nov 06 '18 at 22:42

0 Answers0