简体   繁体   中英

How to order by included model property in LoopBack JS

I have a person model which has a belongsTo relations with a meeting model. I´m doing the query

Person.find({include:['meetings']})

Which gives me a result like this one:

    person:{
        name:"person 1",
        age: 15
        meeting:{
            name: "The meeting",
            date:"June 26, 2019 11:13:00"
        }
    }

What I would like to do is to order the results of the find function by the meeting date. Is there any way I can achieve this on a single query?

I´ve tried this:

Person.find({include:['meeting'],order:"meeting.date DESC"})

But server crashed when trying this. Can anyone help me achieve this?

Try this :

Person.find({
  include:{
    relation: 'meetings',
    scope: {
      order: 'date DESC'
    }
  }
});

Ordering can be done through model.json file as below:

{
    ...
    "scope": {
        "order": "properyName <ASC/DESC>"
    },
    ...
}

By default, ordering is in ascending order so no need to add ASC explicitly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM