0
0
Expressframework~20 mins

CRUD operations with Sequelize in Express - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sequelize CRUD Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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' });
  }
});
A{"name": "Alice", "age": 30}
B{"error": "Failed to create user"}
Cnull
D{"id": 1, "name": "Alice", "age": 30, "createdAt": "<date>", "updatedAt": "<date>"}
Attempts:
2 left
💡 Hint
Sequelize returns the full created instance including auto-generated fields.
state_output
intermediate
2: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);
A35
Bundefined
C30
Dnull
Attempts:
2 left
💡 Hint
The returning: true option returns the updated instance.
🔧 Debug
advanced
2: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 });
  }
});
AThe 'where' clause is missing in the destroy method.
BThe 'id' parameter is not converted to a number.
CThe route method should be 'post' instead of 'delete'.
DThe destroy method does not exist in Sequelize.
Attempts:
2 left
💡 Hint
Sequelize's destroy method requires a 'where' object to specify which records to delete.
📝 Syntax
advanced
2: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.
AUser.findAll({ where: { age: { [Op.gt]: 25 } } })
BUser.findAll({ where: { age: { $gt: 25 } } })
CUser.findAll({ where: { age: '>25' } })
DUser.findAll({ age: { gt: 25 } })
Attempts:
2 left
💡 Hint
Sequelize uses the Op object for operators like greater than.
🧠 Conceptual
expert
2: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?
ASequelize throws a runtime error about missing 'where' clause.
BNo records will be updated because 'where' is missing.
CAll user records in the database will have their age set to 40.
DOnly the first user record will be updated.
Attempts:
2 left
💡 Hint
Without a 'where' clause, Sequelize updates all rows by default.