2016-06-13 87 views
1

當我使用書架將列設置爲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); 
     }); 

有誰知道我怎樣才能獲取更新的記錄,還是讓我知道,如果我不當更新記錄?任何幫助解決這個問題將不勝感激,因爲我不能用舊數據渲染頁面後,用戶剛剛更新它...

回答

1

你正在寫保存數據,如同步代碼,所以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'); 
    }); 
+1

此外'catch'應該是承諾鏈中的最後一個處理程序。 –

+1

約定@RhysvanderWaerden。放置catch()最後使鏈看起來更像一個順序try {} catch(){}。但是由於catch()和'then(null,function(e){...})'相同,這更像是一種風格。 – flaviodesousa

+0

這是不正確的。在你的代碼示例中,如果發生錯誤,將調用'then'處理程序。 'results'參數將獲取前一個處理程序的返回值,在這種情況下,來自catch的未定義。這不是一個樣式問題,它會導致運行時錯誤。 –

相關問題