Error every time I run datastore.runQuery: one of fields Query.query and Query.gql_query must be set

对着背影说爱祢 提交于 2019-12-20 07:01:13

问题


I'm trying to run a simple query against my Google Cloud datastore using google-api-nodejs-client. I want to query for all entities matching a given kind. When I run this query using the "Try it now" tool it works fine:

Request

POST https://www.googleapis.com/datastore/v1beta2/datasets/healthier-staging/runQuery?key={YOUR_API_KEY}

{
 "query": {
  "kinds": [
   {
    "name": "Subscriber"
   }
  ]
 }
}

Response

200 OK

{
 "batch": {
  "entityResultType": "FULL",
  "entityResults": [
   {
    "entity": {
     "key": {
      "partitionId": {
       "datasetId": "s~healthier-staging"
      },
      "path": [
       {
        "kind": "Subscriber",
        "name": "+1215XXXXXXX"
       }
      ]
     },
     "properties": {
...

I'm able to authenticate using my credentials, create a transaction, etc. so I know it's not an authentication issue.

Here's the code I'm trying to run in Node:

this.datastore.runQuery({
    datasetId: 'healthier-staging',
    query: {
        kinds: [{name: 'Subscriber'}]
    },
}, (function(err, result) {
    if (err) {
        console.error(err);
        return;
    }
}).bind(this));

When I try to run the same query using the Node module, I get this error:

{ [Error: one of fields Query.query and Query.gql_query must be set]
  code: 400,
  errors: 
   [ { domain: 'global',
       reason: 'INVALID_ARGUMENT',
       message: 'one of fields Query.query and Query.gql_query must be set' } ] }

This doesn't make sense, since I've specified the query field. I've tried all sorts of things: removing datasetId (produces an error about needing datasetId), using gql_query instead (same error), encapsulating the datasetId inside a transaction and passing that along inside readOptions, etc.

Is this a bug or am I doing something silly?

Thanks!


回答1:


I mentioned this on your other StackOverflow question, but your request should be included in the resource section:

this.datastore.runQuery({
    datasetId: 'healthier-staging',
    resource: {
        query: {
            kinds: [{name: 'Subscriber'}]
        },
    },
}, (function(err, result) {
    if (err) {
        console.error(err);
        return;
    }
}).bind(this));


来源:https://stackoverflow.com/questions/31094523/error-every-time-i-run-datastore-runquery-one-of-fields-query-query-and-query-g

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!