Google datastore.lookup in Node.js returns no resu

2019-09-08 19:28发布

问题:

I'm trying to run a simple lookup against my Google datastore data via the google-api-nodejs-client JS plugin. When I run this lookup using Google's "try it now" tool, it works fine:

Request

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

{
 "keys": [
  {
   "path": [
    {
     "kind": "Subscriber",
     "name": "+1215XXXXXXX"
    }
   ]
  }
 ]
}

Response

200 OK

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

However, when I run exactly the same query in Node.js, I get no results--no error, but also no results. I'm able to authenticate, connect to the datastore, create a transaction, etc. so it doesn't appear to be an authentication issue. Here's the Node code I'm running:

this.datastore.lookup({
    datasetId : 'healthier-staging',
    keys: [{ path: [{ kind: 'Subscriber', name: '+1215XXXXXXX' }] }]
},
    (function(err, result) {
        if (err) {
            console.error(err);
            return;
        }
        console.log(result);
}).bind(this));

And the console output is:

{ found: [], missing: [], deferred: [] }

Note: this is related to the issue I reported in Error every time I run datastore.runQuery: one of fields Query.query and Query.gql_query must be set where I am trying to read the same data using runQuery instead of lookup, but the issue I'm facing is different.

Thanks.

回答1:

The keys should be part of the post payload:

this.datastore.lookup({
    datasetId : 'healthier-staging',
    resource: {
        keys: [{ path: [{ kind: 'Subscriber', name: '+1215XXXXXXX' }] }]
    }
},
    (function(err, result) {
        if (err) {
            console.error(err);
            return;
        }
        console.log(result);
}).bind(this));

You can find the source for the Datastore portion of the API here.