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' }); } });
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.
updatedUser.age after the operation?const updatedUser = await User.update({ age: 35 }, { where: { name: 'Alice' }, returning: true, plain: true }); console.log(updatedUser.age);
returning: true option returns the updated instance.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.
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 }); } });
The destroy method requires a where option to specify the condition. Passing { id: req.params.id } directly is incorrect and causes an error.
Sequelize requires the use of Op.gt inside the where clause to filter by greater than.
await User.update({ age: 40 }); What will be the result?If you omit the where option, Sequelize applies the update to all rows in the table.
