Search code examples
androidnode.jsrestloopbackjsstrongloop

How to include and order an array using the Strongloop Loopback REST API?


I have am including an array in an existing model, by adding the terms {"include" : ["answers"]} in a model called User/Questions. Therefore, if I get the User/Questions model I will get a list of answers. However, now I would like to order the answers in my Question Model based on the dateCreated. To do this how should I edit the terms I place in the REST Strongloop API's filter?

//QuestionModel

[
{
"question": "where is the coffee shop?"
"answers": [
 //answer model included
  {
    "answer": "I am not sure",
    "dateCreated": 4/1/16,
  },
 {
    "answer": "maybe try 5th avenue?",
    "dateCreated": 4/3/16,
  },
  {
    "answer": "oh its by the bakery",
    "dateCreated": 4/2/16,
  }
]

Solution

  • Scope need to be included when When querying related models. So for your example, it would be along the lines of:

     {"include": {"relation": "answers", "scope": {"order" : "dateCreated DESC"}}}