我正在尝试按照 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/