2016-03-24 155 views
1

我有一個返回結果的代碼。通常,當我收到該結果時,我將它發送給客戶端,並沿着它的方式轉換爲純JSON對象。迭代sequelize查詢結果集(Hapijs)

但現在我需要對該結果集進行一些操作,然後在數據庫中進行另一次查找。

我不明白的是結果集的結構。我如何正確地迭代它。我可以使用for循環提取手動值,但我有一種感覺不是這樣做的。

這是返回結果的代碼:

models.Results.findAll({ 
      where: {ProjectId: projectId} 
     }) 
     .then(function (resultset) {    
      //How do I properly iterate over the resultset 
      for(p in resultset){ 

       var a = p; 
       var something; 

      } 


      reply(resultset).code(200); 
     }, function (rejectedPromiseError) { 
      reply(rejectedPromiseError).code(401); 
     }); 

圖像顯示結果在調試模式。它有4個對象:enter image description here

+0

取而代之的是'爲in'循環,嘗試'resultset.forEach(函數(結果){//結果應該是有你要找的(dataValues,hasPrimaryKeys等)屬性的對象});'。 – utamanna

回答

5

當您使用model.findAll時,返回的resultset是一個數組model Instance objects。如果你只想得到有趣的東西(表中的實際值),你可以遍歷resultset並在每個項目上調用get function,傳遞一個值爲plain: true的選項對象。

resultset.forEach((resultSetItem) => { 
    console.log(resultSetItem.get({ 
     plain: true 
    })); 
});