0
0
Expressframework~10 mins

Deleting 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 delete a document by ID using Express and Mongoose.

Express
app.delete('/items/:id', async (req, res) => {
  try {
    await Item.[1](req.params.id);
    res.send('Deleted successfully');
  } catch (error) {
    res.status(500).send(error.message);
  }
});
Drag options to blanks, or click blank then click option'
AfindByIdAndDelete
BdeleteOneById
CremoveById
DdeleteById
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method that does not exist like 'removeById'.
Trying to delete without specifying the ID.
2fill in blank
medium

Complete the code to send a 404 status if the document to delete is not found.

Express
app.delete('/users/:id', async (req, res) => {
  const deletedUser = await User.findByIdAndDelete(req.params.id);
  if (![1]) {
    return res.status(404).send('User not found');
  }
  res.send('User deleted');
});
Drag options to blanks, or click blank then click option'
Areq.params.id
BdeletedUser
CUser
Dres
Attempts:
3 left
💡 Hint
Common Mistakes
Checking req.params.id instead of the deleted document.
Not handling the case when the document is not found.
3fill in blank
hard

Fix the error in the code to delete a document using Express and Mongoose.

Express
app.delete('/posts/:id', async (req, res) => {
  try {
    const result = await Post.[1]({ _id: req.params.id });
    if (result.deletedCount === 0) {
      return res.status(404).send('Post not found');
    }
    res.send('Post deleted');
  } catch (err) {
    res.status(500).send(err.message);
  }
});
Drag options to blanks, or click blank then click option'
AdeleteOne
Bremove
CfindByIdAndDelete
DdeleteMany
Attempts:
3 left
💡 Hint
Common Mistakes
Using remove which is deprecated.
Using findByIdAndDelete which returns the deleted document, not a result with deletedCount.
4fill in blank
hard

Fill both blanks to delete multiple documents matching a condition and send the count of deleted documents.

Express
app.delete('/tasks/completed', async (req, res) => {
  const result = await Task.[1]({ completed: true });
  res.send(`Deleted [2] completed tasks`);
});
Drag options to blanks, or click blank then click option'
AdeleteMany
Bcount
Cresult.deletedCount
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using find which only retrieves documents.
Trying to use count instead of deletedCount.
5fill in blank
hard

Fill all three blanks to delete a document by ID, handle errors, and send appropriate responses.

Express
app.delete('/comments/:id', async (req, res) => {
  try {
    const deleted = await Comment.[1](req.params.id);
    if (![2]) {
      return res.status(404).send('Comment not found');
    }
    res.[3]('Comment deleted');
  } catch (error) {
    res.status(500).send(error.message);
  }
});
Drag options to blanks, or click blank then click option'
AfindByIdAndDelete
Bdeleted
Csend
DdeleteOne
Attempts:
3 left
💡 Hint
Common Mistakes
Using deleteOne with ID directly (needs filter object).
Checking the wrong variable for deletion result.
Using res.sendStatus instead of res.send for success message.