0

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);
2
  • What does "not working" mean? Commented Mar 16, 2019 at 16:50
  • @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> }' Commented Mar 16, 2019 at 18:05

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

2 Comments

Okay thank you, but how should i structure the entity object returned? that's still not clear to me Thank you for your help
Okey perfect i was able to make it working now, thank you much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.