Challenge - 5 Problems
Express Model Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Express model definition code?
Consider this Express model definition using Mongoose. What will be the output when trying to create a new user without the required 'email' field?
Express
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: String, email: { type: String, required: true }, age: Number }); const User = mongoose.model('User', userSchema); async function createUser() { try { const user = new User({ name: 'Alice', age: 30 }); await user.save(); console.log('User saved'); } catch (error) { console.log(error.message); } } createUser();
Attempts:
2 left
💡 Hint
Think about what happens when a required field is missing in a Mongoose model.
✗ Incorrect
Since 'email' is marked as required in the schema, trying to save a user without it triggers a validation error. The error message indicates the missing required field.
📝 Syntax
intermediate2:00remaining
Which option correctly defines a Mongoose model with a nested schema?
You want to define a Mongoose model for a blog post that includes a nested 'author' object with 'name' and 'email'. Which code correctly defines this model?
Attempts:
2 left
💡 Hint
Nested schemas should be defined separately and referenced inside the main schema.
✗ Incorrect
Option D correctly defines a separate schema for 'author' and uses it inside the 'postSchema'. This is the proper way to nest schemas in Mongoose.
🔧 Debug
advanced2:00remaining
Why does this Mongoose model throw a 'Missing schema' error?
Examine the code below. Why does it throw an error 'Missing schema for model "User"' when trying to create a new User?
Express
const mongoose = require('mongoose'); const User = mongoose.model('User'); async function createUser() { const user = new User({ name: 'Bob', email: 'bob@example.com' }); await user.save(); console.log('User saved'); } createUser();
Attempts:
2 left
💡 Hint
Check if the schema is passed when defining the model.
✗ Incorrect
Calling mongoose.model('User') without a schema first causes Mongoose to throw 'Missing schema' error. The schema must be defined and passed when creating the model.
❓ state_output
advanced2:00remaining
What is the value of 'user.isActive' after saving this Mongoose model?
Given this schema with a default value, what will be the value of 'isActive' for a new user saved without specifying it?
Express
const mongoose = require('mongoose'); const userSchema = new mongoose.Schema({ name: String, isActive: { type: Boolean, default: true } }); const User = mongoose.model('User', userSchema); async function createUser() { const user = new User({ name: 'Carol' }); await user.save(); console.log(user.isActive); } createUser();
Attempts:
2 left
💡 Hint
Look at the default value set in the schema for 'isActive'.
✗ Incorrect
The schema sets 'isActive' default to true, so when not provided, Mongoose assigns true automatically.
🧠 Conceptual
expert2:00remaining
Which statement about Mongoose schema options is correct?
Consider these statements about Mongoose schema options. Which one is true?
Attempts:
2 left
💡 Hint
Think about what 'timestamps' option does in Mongoose.
✗ Incorrect
Setting 'timestamps: true' adds 'createdAt' and 'updatedAt' fields automatically and updates them when documents are saved.