0
votes

I'm trying with no success to retrieve my Api key for a service stored as Entity in the Google Cloud Datastore from my NodeJs server running on GAE. I can't find any useful documentation, could someone help me to find out how to retrieve the Entity? Thank you in advance

My Entity on Datastore

My not working code:

const {Datastore} = require('@google-cloud/datastore');
const projectId = 'abcdefghi';
const ds = new Datastore({
  projectId: projectId,
});
const keyName = 'UNSPLASH_KEY';
const kind = 'Strings';
const stringKey = ds.key([kind, keyName]);

var appkey = 'not set';

var entity  = {
key: stringKey,
value: appkey,
};

entity = ds.get(stringKey);
1
What does "not working" mean? - Dan Cornilescu
@DanCornilescu i don't understand which type of structure should i receive from ds.get(), i can't find any useful example, at the moment if i console.log the result of the get() i see 'Promise { <pending> }' - Alex

1 Answers

1
votes

The Promise object you get indicates that the .get() function is an async one and represents the eventual completion (or failure) of that function execution, not its result.

To see the actual result of the function execution (if, of course, it succeeds) you need to use the await operator with it:

entity = await ds.get(stringKey);

This is shown in the Retrieving an entity example:

const [entity] = await datastore.get(taskKey);

As for the structure - the result is a dictionary with an entry for each of the entity's property. You can manually add a property to the entity in the console and you'll see it in the result next time you'll get the entity. From Entities, Properties, and Keys (emphasis mine):

Data objects in Cloud Firestore in Datastore mode are known as entities. An entity has one or more named properties, each of which can have one or more values. Entities of the same kind do not need to have the same properties, and an entity's values for a given property do not all need to be of the same data type. (If necessary, an application can establish and enforce such restrictions in its own data model.)