2017-04-03 8 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 
     } 
    } 
}; 

또한 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