0

I am trying to update optional fields of a farm that belong to a user. For example, if the user only wants to change the name from "Farm 1" to "Farm 99", a JSON will be sent like this:

{
    "name": "Farm 99"
}

this is an example of a user who has two farms:

     }
         "farms": [
            {
                "extension": {
                    "extension": 22,
                    "measure": "Hectares"
                },
                "animals": [],
                "outs": [],
                "plans": [],
                "_id": "5ae64b9f9564b42d31e5bb99",
                "name": "Farm 1",
                "createdAt": "2018-04-29T22:47:59.514Z",
                "updatedAt": "2018-04-29T22:47:59.514Z"
            },
            {
                "extension": {
                    "extension": 50,
                    "measure": "Hectares"
                },
                "animals": [],
                "outs": [],
                "plans": [],
                "_id": "5ae64ceeda4eb82e50502901",
                "name": "Farm 2",
                "updatedAt": "2018-04-29T22:53:34.467Z",
                "createdAt": "2018-04-29T22:53:34.467Z"
            }
        ],
        "_id": "5ae64b9f9564b42d31e5bb98",
        "name": "John",
        "lastName": "Doe",
        "username": "john99",
        "password": "123",
        "createdAt": "2018-04-29T22:47:59.514Z",
        "updatedAt": "2018-04-29T22:53:34.467Z",
        "__v": 0
    }

and this is the code that I'm using for it:

exports.update = (req, res) => { // fix me
  User.update (
    { username: req.params.username, 'farms._id' : req.params.idFarm },
    { $set: {
      farms: req.body
    } }
  )
    .then(farm => {
      if (!farm) {
        return res.status(404).send({
          message: "Farm not found with ID  " + req.params.idFarm
        });
      }
      res.send(farm);

    })
    .catch(err => {
      if (err.kind === "ObjectId") {
        return res.status(404).send({
          message: "Farm not Found with ID " + req.params.idFarm
        });
      }
      return res.status(500).send({
        message: "Error Updating Farm with ID " + req.params.idFarm
      });
    });
};

and this is the result:

"farms": [
            {
                "animals": [],
                "outs": [],
                "plans": [],
                "_id": "5ae659c55dacc836066102f3",
                "name": "Farm 99"
            }
        ],

As you can see, when you try to update all the farms are deleted and only the one that you try to update but with incomplete fields is left.

alex22596
  • 13
  • 4

1 Answers1

0

Because you are updating the entire array, only the updated object appears in the array. You need to use the $set operator together with the $ positional operator.

User.update (
{ username: req.params.username, 'farms._id' : req.params.idFarm },
{ $set: { "farms.$.name": req.body.name } } )

That should only replace only the affected object in the array and update only the name attribute.

If you would like to replace the entire object, try this:

User.update (
{ username: req.params.username, 'farms._id' : req.params.idFarm },
{ $set: { "farms.$": req.body } } )

Hope that helps.

Cheers

tim
  • 358
  • 3
  • 14