Error every time I run datastore.runQuery: one of

2019-03-06 07:44发布

问题:

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));