0

I'm attempting to update a single row of a table within a pug page. The rows of this table are populated from MongoDB. The cells of these table rows are populated using document data e.g. doc.name, while the actual table row contains the document's ID e.g. doc._id.

tr(id = doc._id)
 form.hidden(method = "patch", action = "../update" + doc._id)
  td button.btn(type = "submit") Update
  td
   input(name = "name", value = "doc.name")
  td
   input(name = "age", value = "doc.age")

The route for this operation is shown below:

router.patch("../update/:id", Controller.update)

The controller's update function is:

exports.update = (req, res, next) => {
 const id = req.params.id;
 const operations = {};
 for(const task of req.body){
  operations[task.propName] = task.value;
 }
 Feature.update({_id: id}, {$set: operations})
 .exec()
 .then(
  res.status(200)
  )
 .catch(
 res.status(500)   
 )
}

I'm able to enter and submit the change, then pass the URL back through an Express route ../update/:id so that the request equals GET ../update/testID123?name=testName&age=42 404. I understand that this means the ID can't be found, but I'm perplexed as to why not? It was my understanding that the last section of the URL was represented as req.params and anything after ? was the req.body?

However, when I conduct the identical request using a defined PATCH request via Postman, I receive a different error mentioning that req.body is not iterable, but I don't understand how to force differentiation between the auto-selected GET request and the desired PATCH request. It's worth noting that I've already followed the path to the "not iterable" error as outlaid in this post, so I don't think this is the real problem.

It's the end of the day and my brain has turned to mush, so apologies for missing out any key details, and thanks for reading.

Dbercules
  • 629
  • 1
  • 9
  • 26

1 Answers1

0

Found the answer thanks to advice given in this post. I was accessing aspects of an object that didn't exist, in a manner that wasn't suitable for iteration as outlaid in the linked post. I was wrong in thinking that the req.body not iterable error was a red herring, so I re-implemented the task of Object.keys(req.body) loop and instead of appending [task.propName] = task.value to the updateOperations array, I had to implement [task] = req.body[task].

Dbercules
  • 629
  • 1
  • 9
  • 26