Node.JS Datastore Query get Key

How can I get the key of the object returned by the request?

I tried to access it like normal data, but when I print the object itself, there is no key. Is there any way to do this?

Thank you in advance for your help.

+5
source share
2 answers

Starting with version 0.5.0 @ google-cloud / datastore v0.5.0, the key is now available in Symbol.

var datastore = require('@google-cloud/datastore')(); var query = datastore.createQuery('AnimalNamespace', 'Lion'); query.run(function(err, entities) { var keys = entities.map(function(entity) { // datastore.KEY is a Symbol return entity[datastore.KEY]; }); }); 

You can also use the gstore-node library ( https://github.com/sebelga/gstore-node ) and then you will access it directly through entity.entityKey

+9
source

According to the documentation , datastore.runQuery returns entity objects with data and key properties. Are you interested in key .

 datastore.runQuery(query, function(err, entities) { // entities = [ // { // data: The record, // key: The key for this record // }, // ... // ] }); 

So, for example, using the first object, you will gain access to the key as follows:

 entity = entities[0] key = entity.key 
-1
source

Source: https://habr.com/ru/post/1258842/


All Articles