Challenge - 5 Problems
Sequelize CRUD 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 Sequelize create operation?
Consider this Express route using Sequelize to create a new user. What will be the JSON response after a successful creation?
Express
app.post('/users', async (req, res) => { try { const user = await User.create({ name: 'Alice', age: 30 }); res.json(user); } catch (error) { res.status(500).json({ error: 'Failed to create user' }); } });
Attempts:
2 left
💡 Hint
Sequelize returns the full created instance including auto-generated fields.
✗ Incorrect
Sequelize's create method returns the full instance including auto-generated fields like id, createdAt, and updatedAt. The response sends this full object as JSON.
❓ state_output
intermediate2:00remaining
What is the result of this Sequelize update operation?
Given this code snippet updating a user's age, what will be the value of
updatedUser.age after the operation?Express
const updatedUser = await User.update({ age: 35 }, { where: { name: 'Alice' }, returning: true, plain: true }); console.log(updatedUser.age);
Attempts:
2 left
💡 Hint
The
returning: true option returns the updated instance.✗ Incorrect
With returning: true and plain: true, Sequelize returns an array where the second element is the updated instance. So updatedUser.age is undefined because updatedUser is an array.
🔧 Debug
advanced2:00remaining
Why does this Sequelize delete operation fail with an error?
This code attempts to delete a user but throws an error. What is the cause?
Express
app.delete('/users/:id', async (req, res) => { try { await User.destroy({ id: req.params.id }); res.json({ message: 'User deleted' }); } catch (error) { res.status(500).json({ error: error.message }); } });
Attempts:
2 left
💡 Hint
Sequelize's destroy method requires a 'where' object to specify which records to delete.
✗ Incorrect
The destroy method requires a where option to specify the condition. Passing { id: req.params.id } directly is incorrect and causes an error.
📝 Syntax
advanced2:00remaining
Which option correctly retrieves all users older than 25 using Sequelize?
Select the correct Sequelize query to find all users with age greater than 25.
Attempts:
2 left
💡 Hint
Sequelize uses the Op object for operators like greater than.
✗ Incorrect
Sequelize requires the use of Op.gt inside the where clause to filter by greater than.
🧠 Conceptual
expert2:00remaining
What happens if you call Sequelize's update method without the 'where' option?
Consider this code:
await User.update({ age: 40 }); What will be the result?Attempts:
2 left
💡 Hint
Without a 'where' clause, Sequelize updates all rows by default.
✗ Incorrect
If you omit the where option, Sequelize applies the update to all rows in the table.