使用NodeJs获取Google数据存储区实体 [英] Get Google Datastore entity using NodeJs

查看:65
本文介绍了使用NodeJs获取Google数据存储区实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从运行在GAE上的NodeJs服务器检索作为Entity存储在Google Cloud Datastore中的服务的Api密钥,但没有成功.我找不到任何有用的文档,有人可以帮助我了解如何检索实体吗?预先谢谢你

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

我的无效代码:

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()函数是异步函数,表示最终完成(或失败),而不是结果.

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

这在检索实体示例中显示:

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

Cloud Firestore中数据存储"模式下的数据对象称为实体.一个实体具有一个或多个名为 properties 的属性,每个属性可以具有一个或多个值.同类实体不需要具有相同的属性,以及给定实体的值属性并不需要全部具有相同的数据类型.(如有必要,应用程序可以自己建立并实施此类限制数据模型.)

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

这篇关于使用NodeJs获取Google数据存储区实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆