Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 851
Pablo Estrada Avatar asked Jun 30 '16 23:06

Pablo Estrada


1 Answers

Try this :

Person.find({
  include:{
    relation: 'meetings',
    scope: {
      order: 'date DESC'
    }
  }
});
like image 77
Ebrahim Pasbani Avatar answered Oct 05 '22 11:10

Ebrahim Pasbani