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?
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()]
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.