我有一个前端,它显示从数据库中获取的任务列表以及允许用户更新它的表单。我想在添加新任务后使用新列表更新前端。我尝试在创建查询后执行搜索查询并将信息发送到前端,但它返回以前的值。
app.post(
"/addTask",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Task.create({
description: req.body.data,
user_id: req.user.dataValues.user_id
}).then(
Task.findAll({
where: { user_id: req.user.dataValues.user_id }
}).then(data => {
console.log("HERE: ", data); // logs [], initial value when no tasks
res.status(200).json(data);
})
);
}
);
最佳答案
你可以这样尝试吗?
app.post(
"/addTask",
passport.authenticate("jwt", { session: false }),
(req, res) => {
Task.create({
description: req.body.data,
user_id: req.user.dataValues.user_id
}).then(result => {
Task.findAll({
where: { user_id: req.user.dataValues.user_id }
}).then(data => {
console.log("HERE: ", data); // logs [], initial value when no tasks
res.status(200).json(data);
});
});
}
);
关于mysql - MySQL更新后如何立即检索信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59952506/