Bookshelf.js - 更新记录后,初始页面加载/查询显示旧记录。刷新数据正确显示后
问题描述:
当我使用书架将列设置为null并重定向到显示该数据的页面时,数据将显示未更新的记录,直到我刷新。下面的代码:Bookshelf.js - 更新记录后,初始页面加载/查询显示旧记录。刷新数据正确显示后
Table.where({id: req.param.id}).fetch()
.catch(function (err) {
console.log(err);
})
.then(function (results) {
results.set('other_column', null);
results.save();
results.refresh();
// Redirect to orders page
res.redirect('/landing-page');
});
着陆页的查询是这样的:
Table.where({id: req.param.id}).fetch()
.catch(function (err) {
console.log(err);
})
.then(function (results) {
data.results = results.attributes;
res.render('display-page', data);
});
有谁知道我怎样才能获取更新的记录,还是让我知道,如果我不当更新记录?任何帮助解决这个问题将不胜感激,因为我不能用旧数据渲染页面后,用户刚刚更新它...
答
你正在写保存数据,如同步代码,所以save()可能(并且经常会)在之后刷新()。尝试将其更改为:
Table.where({id: req.param.id}).fetch()
.catch(function (err) {
console.log(err);
})
.then(function (results) {
return results
.set('other_column', null)
.save();
})
.then(function (results) { // results should now include the saved data
// Redirect to orders page
res.redirect('/landing-page');
});
此外'catch'应该是承诺链中的最后一个处理程序。 –
约定@RhysvanderWaerden。放置catch()最后使链看起来更像一个顺序try {} catch(){}。但是由于catch()和'then(null,function(e){...})'相同,这更像是一种风格。 – flaviodesousa
这是不正确的。在你的代码示例中,如果发生错误,将调用'then'处理程序。 'results'参数将获取前一个处理程序的返回值,在这种情况下,来自catch的未定义。这不是一个样式问题,它会导致运行时错误。 –