0
0
Expressframework~20 mins

Mongoose middleware (pre/post hooks) in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mongoose Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a pre-save hook modifies a document field?
Consider a Mongoose schema with a pre('save') hook that changes a field value before saving. What will be the final value stored in the database?
Express
const userSchema = new mongoose.Schema({ name: String, active: Boolean });
userSchema.pre('save', function(next) {
  this.active = true;
  next();
});
const User = mongoose.model('User', userSchema);

const user = new User({ name: 'Alice', active: false });
await user.save();
console.log(user.active);
Aundefined
Btrue
Cfalse
Dnull
Attempts:
2 left
💡 Hint
The pre-save hook runs before the document is saved to the database and can modify fields.
state_output
intermediate
2:00remaining
What is the output of a post-remove hook logging the removed document?
Given a Mongoose model with a post('remove') hook that logs the removed document, what will be printed after calling remove() on a document?
Express
const itemSchema = new mongoose.Schema({ name: String });
itemSchema.post('remove', function(doc) {
  console.log(doc.name);
});
const Item = mongoose.model('Item', itemSchema);

const item = new Item({ name: 'Book' });
await item.save();
await item.remove();
AError: remove is not a function
Bundefined
Cnull
D"Book"
Attempts:
2 left
💡 Hint
The post-remove hook receives the removed document as an argument.
📝 Syntax
advanced
2:00remaining
Which option correctly defines a pre-update hook using async/await?
You want to run code before updating a document using updateOne. Which code snippet correctly defines an async pre-update hook?
Aschema.pre('updateOne', async function(next) { await someAsyncTask(); next(); });
Bschema.pre('updateOne', function(next) { await someAsyncTask(); next(); });
Cschema.pre('updateOne', async function() { await someAsyncTask(); });
Dschema.pre('updateOne', function() { someAsyncTask().then(() => next()); });
Attempts:
2 left
💡 Hint
Async hooks can return a promise or use next callback, but mixing both can cause errors.
🔧 Debug
advanced
2:00remaining
Why does this post-save hook not log the updated document?
A developer writes this post-save hook but it logs the old document state instead of the updated one. Why?
Express
schema.post('save', function(doc) {
  console.log(doc.modifiedField);
});

await Model.findByIdAndUpdate(id, { modifiedField: 'newValue' });
AfindByIdAndUpdate does not trigger save middleware
Bpost-save hook only runs on new documents
CThe hook must be pre-save to see changes
DThe document passed to post-save is a copy, not updated
Attempts:
2 left
💡 Hint
Not all update methods trigger save middleware in Mongoose.
🧠 Conceptual
expert
2:00remaining
How does Mongoose handle errors thrown in async pre hooks?
If an async pre hook throws an error, what is the effect on the document operation?
AThe operation is aborted and the error is passed to the callback or promise rejection
BThe error is ignored and the operation continues normally
CThe error causes the server to crash immediately
DThe hook retries the operation automatically once
Attempts:
2 left
💡 Hint
Middleware errors usually stop the operation and propagate the error.