【问题标题】:Google datastore precondition fail with timestampsGoogle 数据存储先决条件因时间戳而失败
【发布时间】:2016-05-19 22:22:25
【问题描述】:

您好,我正在尝试从 node.js api 查询 google 数据存储条目。我有一个实体,它有一个所有者(字符串)、一个开始时间(日期时间)和一个结束时间(日期时间)我正在尝试查询与给定所有者字符串匹配并在给定日期之后使用以下内容开始的所有实体函数(es2016)。

static async getAvailability (owner, month = currentMonth) {
    const firstOfMonth = moment([currentYear, month])
    const query = datastore.createQuery('availability')
      .filter('owner', '=', owner)
      .filter('end', '>', firstOfMonth.toDate().toJSON())
      .order('end', {
        descending: true
      })
    try {
      // promise version of run query same function
      const result = await datastore.runQueryAsync(query)
      return result.map(result => {
        const { key, data } = result
        data._id = key.id
        return data
      })
    } catch (e) {
      console.log('error', e.stack)
      return []
    }
}

index.yaml 索引

- kind: availability
  properties:
  - name: owner
  - name: start
    direction: desc
  - name: end
    direction: desc

我在运行查询时收到错误前提条件失败错误。如果我能提供更多信息,我会非常高兴。

【问题讨论】:

  • 错误发生在哪一行?
  • datastore.runRequest 上发生错误,所以在此代码行 11 中。

标签: node.js gcloud google-cloud-datastore gcloud-node


【解决方案1】:

您列出的查询仅针对 ownerend。当您使用 Cloud Datastore 时,您使用的索引必须与查询完全匹配。

对于您列出的查询,您需要索引:

- kind: availability
  properties:
  - name: owner
  - name: end
    direction: desc

如果您确实希望您的开始日期是一个特定的时间,您的过滤器必须是:

  .filter('start', '>', firstOfMonth.toDate().toJSON())

还有您would have to specify it first 的订单:

 .order('start')
 .order('end', {
    descending: true
  })

【讨论】:

  • 我工作了 ? 我对定义查询而不是实体的索引感到困惑。
猜你喜欢
  • 1970-01-01
  • 2017-01-08
  • 1970-01-01
  • 2016-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多