node.js - 从本地数据存储检索时未定义实体键和数据属性

标签 node.js google-app-engine google-cloud-datastore gcloud

我正在尝试按照 Google 文档进行操作,这就是我面临的问题。根据文档here当我检索这样的实体时

var key = datastore.key(['Company', 'Google']);

datastore.get(key, function(err, entity) {
    // entity.data = The record.
    // entity.key = The key.
});

我应该获得一个具有键和数据属性的实体对象。那不是我得到的。这是我的阅读功能

function read(kind, id, cb) {
    var key = datastore.key([kind, parseInt(id, 10)]);
    datastore.get(key, (err, entity) => {
    if(err) {
        return cb(err);
    }

    if(!entity) {
        return cb({
            code: 404,
            message: 'Not found'
        });
    }
    cb(null, entity.data);
});
}

数据或 key 均未定义。相反,检索到的实体如下所示

{
  "age": 23,
  "name": "Hello World"
}

这显然只是数据。我究竟做错了什么?我正在开发 gcloud 数据存储区模拟器

这是我的依赖项(如果相关)

"dependencies": {
    "async": "^2.0.1",
    "body-parser": "^1.15.2",
    "express": "^4.14.0",
    "google-cloud": "^0.43.0",
    "yargs": "^5.0.0"
 }

最佳答案

自 @google-cloud/datastore v0.5.0 起,可以通过符号访问 key 。

var datastore = require('@google-cloud/datastore')();

var key = datastore.key(['Company', 'Google']);

datastore.get(key, function(err, entity) {
    var key = entity[datastore.KEY];
});

当然,您始终可以使用gstore-node(免责声明:我是该库的所有者),然后您只需通过entity.entityKey访问它即可。

关于node.js - 从本地数据存储检索时未定义实体键和数据属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40206393/

相关文章:

javascript - 无法在NodeJS中下载页面

node.js - Promise 的 Mocha 单元测试抛出错误

java - 排序和过滤在 Google 应用引擎中不起作用

java - 如何知道 Google AppEngine HRD 数据存储更新何时完成?

arrays - Node : How to store JSON Array in google datastore

javascript - 如何在 findAll 上的 node.js Sequelize 中具有多个关联/条件/包含

java - 想要从 J2SE 应用程序管理 Google 云存储桶

python - 在谷歌应用引擎中运行示例应用程序时出现 "Unable to bind localhost:8000"错误

google-app-engine - 谷歌数据存储全局连接

node.js - 应该如何使用 mongoose 的 model.on ('index' , ...)?