2017-04-03 49 views
0

排序這裏是我當前事件模型SailsJS上執行數據填充

module.exports = { 
    attributes: { 
     name: { 
      type: 'string', 
      required: true, 
      unique: true 
     }, 
     client: { 
      model: 'client', 
      required: true 
     }, 
     location: { 
      model: 'location', 
      required: true 
     } 
    } 
}; 

客戶端型號:

module.exports = { 
    attributes: { 
     name: { 
      type: 'string', 
      required: true, 
      unique: true 
     }, 
     address: { 
      type: 'string' 
     }, 
     clientContact: { 
      model: 'user', 
      required: true 
     } 
    } 
}; 

那麼,如何實現排序基礎上,client name並且還有skiplimit財產(用於分頁)與它一起工作。

我嘗試使用下面的查詢:

Event.find({ id: ['583eb530902db3d926345215', '583eb6dd91b972ee26dd97b1'] }, 
      { select: ['name', 'client'] }) 
      .populate('client', { sort: 'name DESC' }) 
      .exec((err, resp) => resp.map(r => console.log(r.name, r.client))); 

但是,這似乎並沒有做到這一點。

+0

我相信你在找什麼是這裏:http://stackoverflow.com/questions/22996210/sort-by-the-association-with-populate –

回答

2

水線不支持按照這樣的子記錄對結果進行排序。如果client是附在事件子記錄集合,那麼你的代碼將排序每個返回Event記錄的人口client數組的名字,但我假設在這種情況下client是一個奇異的關聯(即model: 'client')。

如果你想要的是Event記錄通過他們的客戶的名稱排序的數組,你可以做到這一點相對容易地使用Lodash您檢索記錄後:

var _ = require('lodash'); 

Event.find(queryCriteria).populate('client').exec((err, resp) => { 
    if (err) { /* handle error */ } 
    var sortedRecords = _.sortBy(eventRecords, (record) => { return record.client.name; }); 
}); 
+0

但Lodash不會讓我分頁數據。不管怎麼說,還是要謝謝你。 – Panic