Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to update a document by its ID using Express and Mongoose.
Express
app.put('/items/:id', async (req, res) => { try { const updatedItem = await Item.findByIdAndUpdate(req.params.id, [1], { new: true }); res.json(updatedItem); } catch (error) { res.status(500).send(error.message); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.params instead of req.body for update data
Forgetting to set { new: true } to return the updated document
✗ Incorrect
To update a document, you pass the new data from the request body to the update function.
2fill in blank
mediumComplete the code to update a document and handle the case when the document is not found.
Express
app.put('/users/:id', async (req, res) => { try { const user = await User.findByIdAndUpdate(req.params.id, req.body, { new: true }); if (!user) { return res.status([1]).send('User not found'); } res.json(user); } catch (error) { res.status(500).send(error.message); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 400 or 500 status codes instead of 404 for not found
Not returning after sending the 404 response
✗ Incorrect
If the document is not found, the server should respond with a 404 Not Found status.
3fill in blank
hardFix the error in the code to update a document using Mongoose's updateOne method.
Express
app.patch('/posts/:id', async (req, res) => { try { const result = await Post.updateOne({ _id: [1] }, req.body); res.json(result); } catch (error) { res.status(500).send(error.message); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body.id which may not exist
Using a wrong parameter name like req.params.postId
✗ Incorrect
The document ID is in req.params.id, so use it to find the document to update.
4fill in blank
hardFill both blanks to update a document and return the updated document using findOneAndUpdate.
Express
app.put('/products/:id', async (req, res) => { try { const product = await Product.findOneAndUpdate({ _id: [1] }, [2], { new: true }); if (!product) { return res.status(404).send('Product not found'); } res.json(product); } catch (error) { res.status(500).send(error.message); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.query instead of req.body for update data
Using wrong parameter name for ID
✗ Incorrect
Use req.params.id to find the product and req.body for the update data.
5fill in blank
hardFill all three blanks to update a document with validation and return the updated document.
Express
app.patch('/orders/:id', async (req, res) => { try { const order = await Order.findByIdAndUpdate([1], [2], { new: true, runValidators: [3] }); if (!order) { return res.status(404).send('Order not found'); } res.json(order); } catch (error) { res.status(400).send(error.message); } });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not enabling runValidators, so invalid data can be saved
Using wrong parameter names for ID or update data
✗ Incorrect
Use req.params.id to find the order, req.body for update data, and set runValidators to true to enforce schema validation.