1

I am trying to query the user by the Datastore auto-generated ID.

Below GQL query is able to run on Datastore Console
select * from User where __key__ = Key(User, 123)

But JS code using Datastore JS Library, doesn't work and returning empty list.

const filterKey = dataStore.key(['User', 123]);

dataStore.runQuery(dataStore.createQuery(relationEntity).filter('__key__', filterKey));

1 Answer 1

1

Something I see with your query is that you are missing the comparison operator inside the filter clause. Aside from that, what does relationEntity hold? To mimic your GQL query, it should be the kind name of the entities you want to fetch. I made this query to fetch entities based on your GQL query select * from User where __key__ = Key(User, 123):

const { Datastore } = require("@google-cloud/datastore");

const datastore = new Datastore();
//Kind = “User”, ID = 5634161670881280, using [default] namespace

const entKey = datastore.key(["User", 5634161670881280]);
const query = datastore.createQuery("User").filter("__key__", "=", entKey); 

datastore.runQuery(query).then(([entities]) => {
    entities.forEach((entity) => {
        console.log(entity.firstname) // prints “Foo” which is the ‘firstname’ in my entity
    });
}).catch((err) => {
    console.log(err);
});

Something else that could be a cause is the JS number limit, and in the documentation, it’s mentioned that you would need to wrap your auto-generated ID inside datastore.int(“<YOUR_ID>”) for large numbers. If you’d like to see more sample code, there are a lot of snippets in the Datastore Github repository. Let me know if this was useful.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.