0
votes

Trying to get entities from my google cloud datastore, filtered by a key name. Not sure what am I doing wrong here in my code.

I create my datastore

const ds = new Datastore({
  projectId: "my-project",
  keyFilename: "./servicekey.json",
  namespace: "client-dummy",
});

Then create a key for filtering

  const _key = ds.key({
    namespace: "client-dummy",
    path: ["transactions", data.id],
  });

create my query by supplying the namespace, kind and key based filter.

const query = ds
    .createQuery("client-dummy", "transactions")
    .filter("__key__", _key);

And finally run the query

const result = await ds.runQuery(query);

But results are always empty

[ [], { moreResults: 'NO_MORE_RESULTS', endCursor: 'CgA=' } ]

I have tried running same queries on the UI and get results. I don't know what's wrong in the code. Can someone help?

1
Are you sure that such object exist? What is data.id?vitooh
Yes, I'm sure. Because I made the same query on the datastore UI and get expected results. data.id is correct. I printed it on console log.Ankur Lathwal

1 Answers

0
votes

I have replicated the same code on my side, and was having the same issue. To be able to replicate I have mocked the data.id as const data = {id: 1}, then created an entity to match the use case and indeed had the same behavior.

After playing little while with it, I noticed in UI (Datastore in GCP) in “Edit entity” that Key literal is Key(transactions, '1') which means that this ‘1’ is a String.

Just adding toString() method helped. So key creation looks like this:

const _key = ds.key({
    namespace: "client-dummy",
    path: ["transactions", data.id.toString()]
  });