简体   繁体   English

Node.JS数据存储区查询获取密钥

[英]Node.JS Datastore Query get Key

How can I get the key of an entity returned by a query? 如何获取查询返回的实体的密钥?

I tried to access it like the normal data, but when I print the entity itself there is no key. 我试图像普通数据一样访问它,但是当我打印实体本身时没有密钥。 Is there even a way to do so? 有没有办法这样做?

Thank you in advance for your help. 预先感谢您的帮助。

Since version 0.5.0 of the @google-cloud/datastore v0.5.0 the key is now accesible by a Symbol. 从@ google-cloud / datastore v0.5.0版本0.5.0开始,密钥现在可由符号访问。

var datastore = require('@google-cloud/datastore')();
var query = datastore.createQuery('AnimalNamespace', 'Lion');
query.run(function(err, entities) {

var keys = entities.map(function(entity) {
        // datastore.KEY is a Symbol
        return entity[datastore.KEY];
    });
});

You could also use the gstore-node library ( https://github.com/sebelga/gstore-node ) and then you would access it directly by entity.entityKey 您也可以使用gstore-node库( https://github.com/sebelga/gstore-node ),然后直接通过entity.entityKey访问它。

According to the documentation , datastore.runQuery returns a entity objects, with the properties data and key . 根据文档datastore.runQuery返回一个实体对象,其中包含属性datakey You are interested in key . 你对key感兴趣。

datastore.runQuery(query, function(err, entities) {
  // entities = [
  //   {
  //     data: The record,
  //     key: The key for this record
  //   },
  //   ...
  // ]
});

So, for example using the first entity, you would access the key like this: 因此,例如使用第一个实体,您可以像这样访问密钥:

entity = entities[0]
key = entity.key

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM