2017-04-03 35 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