0
0
Expressframework~10 mins

Updating documents in Express - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Areq.params
Breq.headers
Creq.query
Dreq.body
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
2fill in blank
medium

Complete 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'
A500
B400
C404
D200
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
3fill in blank
hard

Fix 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'
Areq.body.id
Breq.params.id
Creq.query.id
Dreq.params.postId
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.body.id which may not exist
Using a wrong parameter name like req.params.postId
4fill in blank
hard

Fill 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'
Areq.params.id
Breq.body
Creq.query
Dreq.params.productId
Attempts:
3 left
💡 Hint
Common Mistakes
Using req.query instead of req.body for update data
Using wrong parameter name for ID
5fill in blank
hard

Fill 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'
Areq.params.id
Breq.body
Ctrue
Dfalse
Attempts:
3 left
💡 Hint
Common Mistakes
Not enabling runValidators, so invalid data can be saved
Using wrong parameter names for ID or update data