2017-04-03 3 views
0

にソート実行します。SailsJSは、ここに私の現在の<strong>イベントモデル</strong>です移入データ

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

クライアントモデル:私はclient nameに基づいてソートを実装するのですかそう

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

また、skipおよびlimit(ページネーションのための)プロパティがそれと共に働くようにします。

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

関連する問題

 関連する問題