0

playing around with amplify and datastore with nodejs and react.

I have model for message

type Message
@model
@auth(rules: [{allow: owner, operations: [read, create, update, delete]}]) {
  id: ID!
  content: String!
}

I want to fetch all messages and sort in desceding order. What I am trying to do

    const messages = await DataStore.query(Message, Predicates.ALL, {
      sort: s => s.createdAt(SortDirection.DESCENDING),
      page: 0,
      limit: 20
    });

My solution doesn't seem to affect the sorting at all. What am I missing?

Ruben Nagoga
  • 2,178
  • 1
  • 19
  • 30

1 Answers1

0

The createdAt method takes in a string either "ASCENDING" or "DESCENDING"

Try this:

const messages = await DataStore.query(Message, Predicates.ALL, {
      sort: s => s.createdAt("DESCENDING"),
      page: 0,
      limit: 20
    });
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 05 '22 at 23:09