【问题标题】:GQL Functions and Datastore Datetime query troubleGQL 函数和数据存储日期时间查询问题
【发布时间】:2019-04-09 05:05:03
【问题描述】:

这是上下文:

我有一个 GCP 函数,它必须去 Datastore 获取一些数据才能将数组返回给客户端。

问题:

当我对我的代码使用日期时间过滤器时,我无法实现 GCP 函数返回数据,但是,当我在 GCP 数据存储查询控制台上放置等效查询时,我可以实现返回很多行。

技术数据:

数据存储区 GQL:

select * from KIND where recordDate >= DATETIME ("2018-10-10T10:10:00.000000+03:00") and recordDate <= DATETIME ("2018-10-11T10:10:00.999999+03:00")

(适用于 GCP 数据存储控制台)

GCP 功能代码:

    query = datastore.createQuery(kind).filter('recordDate','>=',dateFrom).filter('recordDate','<=',dateTo);
    console.log(query);

    datastore.runQuery(query, (err,entities) => {
        console.log(err);
        console.log(entities);
    });

(它 runQuery()... 总是返回 null 作为 err 变量并在实体变量上返回一个 void 数组)

我需要的帮助:

谁能告诉我一个成功的查询示例 使用日期时间过滤器返回实体?


我尝试了关于 dateFrom 和 dateTo vars 格式的方法:

  • 日期时间(“2018-10-10T10:10:00.000000+03:00”)
  • 日期时间(“2018-10-10 10:10:00”)
  • “2018-10-10T10:10:00.000000+03:00”
  • '2018-10-10T10:10:00.000000+03:00'
  • 日期时间(“2018-10-10”)
  • “2018-10-10”
  • 日期(“2018-10-10”)
  • 日期('2018-10-10')
  • 日期时间 (2018-10-10T10:10:00.000000+03:00)

没有人工作:(

更新(2018-11-19):

我在 runQuery 之前打印了查询,我得到了这个: (我把一些点放在安全的敏感数据上)

    {
      "textPayload": "Query {\n  scope: \n   Datastore {\n     clients_: Map {},\n     datastore: [Circular],\n     namespace: undefined,\n     projectId: '................',\n     defaultBaseUrl_: 'datastore.googleapis.com',\n     baseUrl_: 'datastore.googleapis.com',\n     options: \n      { libName: 'gccl',\n        libVersion: '2.0.0',\n        scopes: [Array],\n        servicePath: 'datastore.googleapis.com',\n        port: 443,\n        projectId: 'c..........' },\n     auth: \n      GoogleAuth {\n        checkIsGCE: undefined,\n        jsonContent: null,\n        cachedCredential: null,\n        _cachedProjectId: 'c..........',\n        keyFilename: undefined,\n        scopes: [Array] } },\n  namespace: null,\n  kinds: [ '....KIND......' ],\n  filters: \n   [ { name: 'recordDate', op: '>', val: 2018-10-10T00:00:00.000Z },\n     { name: 'recordDate', op: '<', val: 2018-10-12T23:59:59.000Z } ],\n  orders: [],\n  groupByVal: [],\n  selectVal: [],\n  startVal: null,\n  endVal: null,\n  limitVal: 20,\n  offsetVal: -1 }",
      "insertId": "............................098...",
      "resource": {
        "type": "cloud_function",
        "labels": {
          "region": "us-central1",
          "function_name": "...................-get-search",
          "project_id": "............."
        }
      },
      "timestamp": "2018-11-19T21:19:46.737Z",
      "severity": "INFO",
      "labels": {
        "execution_id": "792s.....lp"
      },
      "logName": "projects/......./logs/cloudfunctions.googleapis.com%2Fcloud-functions",
      "trace": "projects/........../traces/4a457.......",
      "receiveTimestamp": "2018-11-19T21:19:52.852569373Z"
}

功能代码是:

query = datastore.createQuery(kind).filter('recordDate','>',new Date(dateFrom)).filter('recordDate','<',new Date(dateTo)).limit(20);

console.log(query);

var test = datastore.runQuery(query, (err,entities) => {

    console.log(err);
    console.log(entities);

    entities.forEach(entity => {
        console.log(entity);
      });
      return{
          entities:entities,
          err:err
      };
});
console.log(test);

【问题讨论】:

    标签: datetime google-cloud-platform google-cloud-datastore google-cloud-functions gql


    【解决方案1】:

    当使用客户端库根据日期时间属性过滤或排序查询结果时,您应该使用相应语言的本地日期时间表示,而不是字符串或 GQL 结构。

    特别是对于您显然使用的 node.js,您应该使用 Date() 对象。以下是来自Restrictions on queries 的示例:

    const query = datastore
      .createQuery('Task')
      .filter('created', '>', new Date('1990-01-01T00:00:00z'))
      .filter('created', '<', new Date('2000-12-31T23:59:59z'));
    

    【讨论】:

    • 我也试过这种方式我的朋友......当你在这里发布这个时我再次尝试,因为我相信我疯了但不是......查询什么都不做......我无法理解为什么呢。我向谷歌的人提问,他们回复我......我会对此进行评估,然后我会告诉大家解决这个问题的正确方法。感谢您的答复。 :)
    • 谷歌工作人员说你有正确的方法......所以我打印了查询以显示更多数据。请检查这个(我会更新标题)
    • 它不起作用,因为它返回一个 void 数组作为响应,并且错误变量上没有任何内容。好奇怪
    猜你喜欢
    • 2020-01-20
    • 2011-02-08
    • 2019-07-30
    • 2016-11-11
    • 1970-01-01
    • 2010-12-20
    • 1970-01-01
    • 2015-12-22
    • 2015-01-01
    相关资源
    最近更新 更多