异步等待然后在同一功能上
Async await and then on the same function
使用 Sequelize 有更好的方法吗?
关键是,我怎么知道更新是否在不使用 .then 和 .catch 的情况下工作,因为我已经在使用内部和异步功能,我需要验证更新是否真的有效。
async update(req, res) {
const owner = await Owner.findByPk(req.params.ownerId);
if (owner) {
const { name, email, password, type } = req.body;
owner.update({ name, email, password, type })
.then(result => {
return res.json({ status: 'success', message: 'Owner updated' });
})
.catch(err => {
return res.status(500).json({ status: 'error', message: err });
});
}
return res.status(404).json({ status: 'not found', message: 'Owner not found' });
},
改用async
/await
语法
try {
await owner.update({ name, email, password, type });
return res.json({ status: 'success', message: 'Owner updated' });
} catch (err) {
return res.status(500).json({ status: 'error', message: err });
}
使用 Sequelize 有更好的方法吗? 关键是,我怎么知道更新是否在不使用 .then 和 .catch 的情况下工作,因为我已经在使用内部和异步功能,我需要验证更新是否真的有效。
async update(req, res) {
const owner = await Owner.findByPk(req.params.ownerId);
if (owner) {
const { name, email, password, type } = req.body;
owner.update({ name, email, password, type })
.then(result => {
return res.json({ status: 'success', message: 'Owner updated' });
})
.catch(err => {
return res.status(500).json({ status: 'error', message: err });
});
}
return res.status(404).json({ status: 'not found', message: 'Owner not found' });
},
改用async
/await
语法
try {
await owner.update({ name, email, password, type });
return res.json({ status: 'success', message: 'Owner updated' });
} catch (err) {
return res.status(500).json({ status: 'error', message: err });
}