SailsJS 对填充数据执行排序

SailsJS Perform sorting on populate data

这是我当前的事件模型

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)));

但这好像不行。

Waterline 不支持按这样的子记录对结果进行排序。如果 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; });
});