【问题标题】:RxDb, cannot sort on field(s) XXX when using the default indexRxDb,使用默认索引时无法对字段 XXX 进行排序
【发布时间】:2020-06-09 18:02:57
【问题描述】:

我刚刚发现了 RxDb,据我所知,它在下面运行 PouchDB,无论如何,我已经为实体定义了一个相当简单的架构:

let nodeSchema: RxJsonSchema<NodeDocType> = {
  version: 0,
  title: `node schema`,
  description: `Describes a node`,
  type: `object`,
  properties: {
    id: {
      type: `string`,
      primary: true,
    },
    type: {
      type: `string`,
    },
    createdAt: {
      type: `number`,
    },
    updatedAt: {
      type: `number`,
    },
    text: {
      type: `string`,
    },
    name: {
      type: `string`,
    },
    originalUrl: {
      type: `string`,
    },
    localUrl: {
      type: `string`,
    },
    webUrl: {
      type: `string`,
    },
    extension: {
      type: `string`,
    },
  },
  required: [`type`, `createdAt`, `updatedAt`],
}

现在的问题是,当我从组件中查询集合并尝试应用排序时,如下所示:

db.nodes
      .find()
      .sort({
        createdAt: `asc`
      })
      .$.subscribe((nodes) => {
        if (!nodes) {
          return
        }
        setNodes(nodes)
      })

我收到一条错误消息cannot sort on field(s) XXX when using the default index,从我目前看到的情况来看,这与字段不是选择器的一部分有关吗?但它仍然令人困惑并且无法使其正常工作,有人知道我做错了什么吗?

【问题讨论】:

    标签: javascript pouchdb rxdb


    【解决方案1】:

    https://rxdb.info/rx-schema.html#indexes

    您需要修改架构以包含要排序的属性的索引。

    let nodeSchema: RxJsonSchema<NodeDocType> = {
      version: 0,
      title: `node schema`,
      description: `Describes a node`,
      type: `object`,
      properties: {
        createdAt: {
          type: `number`,
        },
      },
      required: [`type`, `createdAt`, `updatedAt`],
      indexes: ['createdAt']
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 2018-07-09
      相关资源
      最近更新 更多