使用 NodeJs 获取 Google 数据存储实体

Get Google Datastore entity using NodeJs

我正在尝试从我在 GAE 上的 NodeJs 服务器 运行 检索存储为 Google 云数据存储中的实体的服务的 Api 密钥,但没有成功。 我找不到任何有用的文档,有人可以帮我找出如何检索实体吗? 提前谢谢你

我的无效代码:

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

您获得的 Promise 对象表示 .get() 函数是一个异步函数,代表该函数执行的 最终完成(或失败) ,而不是它的结果。

要查看函数执行的实际结果(当然,如果它成功的话)你需要使用 await operator:

entity = await ds.get(stringKey);

这显示在 Retrieving an entity 示例中:

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

至于结构 - 结果是一个字典,每个实体的 属性 都有一个条目。您可以手动将 属性 添加到控制台中的实体,下次获取该实体时,您会在结果中看到它。来自 Entities, Properties, and Keys(强调我的):

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