Google datastore.lookup in Node.js returns no results

旧巷老猫 提交于 2019-12-25 02:51:00

问题


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.



来源:https://stackoverflow.com/questions/31096115/google-datastore-lookup-in-node-js-returns-no-results

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