Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.JS Datastore Query get Key

How can I get the key of an entity returned by a query?

I tried to access it like the normal data, but when I print the entity itself there is no key. Is there even a way to do so?

Thank you in advance for your help.

like image 727
ReasoN Avatar asked Oct 26 '16 17:10

ReasoN


1 Answers

Since version 0.5.0 of the @google-cloud/datastore v0.5.0 the key is now accesible by a 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 could also use the gstore-node library (https://github.com/sebelga/gstore-node) and then you would access it directly by entity.entityKey

like image 99
Sébastien Loix Avatar answered Nov 16 '22 18:11

Sébastien Loix