2017-06-06 131 views
1

下面的代碼在執行時返回空結果。沒有Count查詢就沒問題,但即使如此,仍會返回正確的值,但響應始終爲空。我有比賽條件問題嗎?在count()後查詢返回Nothing查詢

module.exports = { 
getSites: function (req, res) { 
    var sites = []; 
    Site.find({ company: req.session.companyID }).populate('locations').exec(function afterFind(err, results) { 

     for (index in results) { 

      var siteObj = {}; 

      siteObj = results[index]; 


      sails.models['site_users__user_sites'].count({ user_sites: results[index].id }).exec(function found(err, counted) { 
       if (err) { 
        sails.log.error(err); 
       } 
       sails.log.debug(counted); // Prints correct number 
       siteObj['user_count'] = counted; 
       sails.log.debug(siteObj['user_count']); // Shows value is present 
       sites.push(siteObj); 
      }); 
     } 
    }); 

    sails.log.debug(sites); // Nothing 
    return res.json(sites); // Nothing 
},} 

更新:

module.exports = { 
getSites: function (req, res) { 
    var sites = []; 

    Site.find({ company: req.session.companyID }).populate('locations').exec(function afterFind(err, results) { 
     var completedResults = 0; 

     for (index in results) { 
      sails.log.debug(results); 
      var siteObj = {}; 

      siteObj = results[index]; 

      sails.models['site_users__user_sites'].count({ user_sites: results[index].id }).exec(function found(err, counted) { 
       if (err) { 
        sails.log.error(err); 
       } 
       siteObj['user_count'] = counted; 
       sites.push(siteObj); 

       completedResults++; 
       if(completedResults === results.length) { 
        sails.log.debug(sites); 
        res.json(sites); 
       } 
      }); 
     } 
    }); 
}, 
} 

更新上面,記錄 「成果」 之後生產的所有正確的記錄。但是,返回「網站」時,返回的兩個條目是重複的。

更新2

我有一種感覺,異步查詢是不是下一個項目之前,在for循環中被訪問的完成,因此分配值給錯了對象。

更新3 提供幫助日誌。

這裏是日誌的 「結果」

[ { locations: [], 
company: 1, 
name: 'N', 
id: 1, 
createdAt: '2017-06-06T10:07:25.000Z', 
updatedAt: '2017-06-06T10:07:26.000Z' }, 
{ locations: [], 
company: 1, 
name: 'W', 
id: 2, 
createdAt: '2017-06-06T11:08:08.000Z', 
updatedAt: '2017-06-06T11:08:08.000Z' } ] 

這是日誌的 「地王」

[ { locations: [], 
company: 1, 
name: 'W', 
id: 2, 
createdAt: '2017-06-06T11:08:08.000Z', 
updatedAt: '2017-06-06T11:08:08.000Z', 
user_count : 1 }, 
{ locations: [], 
company: 1, 
name: 'W', 
id: 2, 
createdAt: '2017-06-06T11:08:08.000Z', 
updatedAt: '2017-06-06T11:08:08.000Z', 
user_count : 1 } ] 

回答

0

這是因爲

sails.log.debug(sites); // Nothing 
return res.json(sites); // Nothing 

sails.models['site_users__user_sites'].count({ user_sites: results[index].id }).exec(function found(err, counted) { 
    if (err) { 
     sails.log.error(err); 
    } 
    sails.log.debug(counted); // Prints correct number 
    siteObj['user_count'] = counted; 
    sails.log.debug(siteObj['user_count']); // Shows value is present 
    sites.push(siteObj); 
}); 
前執行

要解決該問題,您需要在所有結果的異步執行功能完成後致電sails.log.debugres.json。就像這樣:

module.exports = { 
    getSites: function (req, res) { 
     const sites = []; 
     Site.find({ company: req.session.companyID }).populate('locations').exec(function afterFind(err, results) { 

      let completedResults = 0; 

      for (index in results) { 

       const siteObj = results[index]; 


       sails.models['site_users__user_sites'].count({ user_sites: results[index].id }).exec(function found(err, counted) { 
        if (err) { 
         sails.log.error(err); 
        } 
        sails.log.debug(counted); // Prints correct number 
        siteObj['user_count'] = counted; 
        sails.log.debug(siteObj['user_count']); // Shows value is present 
        sites.push(siteObj); 

        completedResults++; 
        if(completedResults === results.length) { 
         sails.log.debug(sites); 
         res.json(sites); 
        } 
       }); 
      } 
     }); 

    } 
} 
+0

這個工程並正確返回,除了現在我收到重複的數據。在這種情況下,我只檢索2個站點記​​錄(在第一個查詢後正確),但是響應與最後一個條目重複。 –

+0

@BenBrookes你有沒有例子? – dan

+0

我已更新我的原始帖子。 –

0

發現,計數和.exec在本質上是異步 您返回站點變量,它是initally空數組,計算你的一個afterFind功能甚至之前,就更不用說了第二Exec之後計數。