【问题标题】:AWS Amplify datastore query and sort by dateAWS Amplify 数据存储查询和按日期排序
【发布时间】:2021-09-12 21:13:05
【问题描述】:

使用 nodejs 进行放大和数据存储并做出反应。

我有message的模型

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

我想获取所有消息并按降序排序。 我想做什么

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

我的解决方案似乎根本不影响排序。我错过了什么?

【问题讨论】:

    标签: aws-lambda aws-amplify aws-amplify-cli


    【解决方案1】:

    createdAt 方法接收字符串“ASCENDING”或“DESCENDING”

    试试这个:

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

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2021-06-04
    • 1970-01-01
    • 2020-01-31
    • 2015-05-08
    • 2020-10-19
    • 2016-11-11
    • 1970-01-01
    • 2021-08-13
    • 2016-10-03
    相关资源
    最近更新 更多