简体   繁体   中英

Access google cloud datastore keys on the frontend

I wrote a get query on the node/express to fetch all the entities of a kind. The get request is successful and i see all the properties returned back as json which i can monitor on the front end angular site. But i do not see NAME/ID property which is basically the keys associated with the enitities. I need the NAME/ID or keys of the entities to perform a different operation. How can i extract the keys for accessing on the front end UI/html?

GET query which i used -

app.get("/api/venues/", (req, res, next) => {
    const query = datastore
    .createQuery('venue');

    query.run().then((venuesList) => {
    // do something
  })

Results i get back after i run the node get -

[ { property1: '<property-value-1>',
    property2: '<property-value-2>',
    property3: '<property-value-3>',
    property4: '<property-value-4>',    
    [Symbol(KEY)]:
     Key {
       namespace: undefined,
       id: <'id-value'>,
       kind: 'venue',
       path: [Getter] } } ]

I am not able to access or see the 'Key' obtained in the above get results on the front end.

Seems like there are very few contributors for google cloud datastore queries on stack overflow. Anyways... i did lot of reserach and came up with the below solution for by question and it is working.

The get request through node will fetch all the entities from datastore and all these entities along with their keys would be visible on the node interface. However, the keys of the entities would not be visible on the UI(Angular, which i am using) eventough we pass on the same json(with entities) to the UI.

So i made a modification on my json obtained from datastore. I extracted the keys of entities seperately and created a new attribute within the same json and passed on this modified json to the UI. So now, i was able to access the keys of entities through an alias attribute which i created at node. Check the modified code below -

app.get("/api/venues/", (req, res, next) => {
    const query = datastore
    .createQuery('venue');

    query.run().then(([venuesList]) => {
    venuesList.forEach(venue => { venue['venueKey'] = venue[datastore.KEY] });
    //venuesList.forEach(venue => console.log(venue));
    res.status(200).json(
      {
        message: "Request was processed successfully!",
        venues: venuesList
      }
    );
  })
})

I hope this helps others in the community.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM