We need to update the existing tree record with new values, ensuring the id from the URL matches the id in the request body. If a mismatch or a non-existent id occurs, handle it gracefully.
req.params.id matches req.body.id; if not, return an error.Tree.findByPk to get the existing record.File Name & Location: server/routes/trees.js
// PUT /trees/:id pseudocode:
// 1. if (req.params.id !== req.body.id) return error
// 2. let tree = await Tree.findByPk(req.params.id)
// 3. if (!tree) return error (not found)
// 4. tree.tree = req.body.name
// 5. tree.location = req.body.location
// 6. etc...
// 7. await tree.save()
// 8. respond with updated tree
Expected Input: A PUT request with both URL param :id and id in the JSON body, plus updated fields (name, location, etc.).
Expected Output: JSON object with the updated tree or an error JSON if mismatch or not found.
Step-by-Step Directions:
PUT /trees/:id.req.params.id and req.body.id.await tree.save().Explanations & Analogies: Updating is like editing the details of a saved library entry. If you reference the wrong ID, you can’t edit that entry.
Real World Example: Editing an item in an online store to fix its name or price.
Confirm the correct record was updated by fetching it again. Test with invalid IDs or mismatched IDs to ensure the correct error response.