cloud_traveler
cloud_traveler

Reputation: 306

How to access Google Datastore IDs on the front end when info is returned via API response in Express?

I am working with React.js, Node + Express for API server, and Google Datastore. I would like to query the Datastore and return back to front end the list of entities and their DB IDs.

When I run a regular Datastore query, the list of entities is returned without the IDs.

Based on https://cloud.google.com/nodejs/docs/reference/datastore/1.4.x/Query I found that to access the DB IDs I need to use results[0][x][Datastore.KEY] to access each ID. Such approach is fine on the back end. However, how would you access the IDs on the front-end?

I can send the query output to front-end using

app.get('/example_url', (req, res) => {
    res.setHeader('Content-Type', 'application/json');
    const query = datastore.createQuery('entity');
     datastore.runQuery(query).then(results => {
        res.send(JSON.stringify( {results }));
.....});

However, on the front end I can't access the IDs. I can't use the notation given for the back end. results[0][x][Datastore.KEY] datastore is not defined on the front end. Not sure what to do.

Upvotes: 0

Views: 235

Answers (2)

cloud_traveler
cloud_traveler

Reputation: 306

The only solution I found so far is: on the back end iterate over the received key.id array/object. put these "id"s in a new array and send it to the front end together with the rest of the DB query results. Then the front end will have access to these "id"s.

Upvotes: 0

ThisIsMyName
ThisIsMyName

Reputation: 917

I praticed Python in App Engine, so my answer won't be complete.

In Python you have to pass the key as a string to the front-end like that: my_key = YourObjectModel.key.urlsafe()

To do it in JS, I think it should be: YourObjectModel.key.id

You have an example at the end of this tutorial

Upvotes: 1

Related Questions