父实体,nodejs

时间:2018-09-16 01:59:03

标签: google-cloud-datastore

伙计们,   一个人如何访问父实体?假设我有2种users并说cars ...当按板查询类型cars时,它将返回与该板匹配的所有实体。如何获得这些实体的祖先信息?如果我想获取返回的实体的父键怎么办?这可能吗?

我只能检索实体的示例

const query = datastoreClient.createQuery('cars')
    .filter('plate', '=', 'ABC');
return datastoreClient.runQuery(query).then(function (entities) {
    console.log(entities);
    return entities;
});

输出:

[
    [
        {
            "id": "2b49ca40-a5fb-11e7-ad5c-c95aa76161c6",
            "plate": "ABC"
        }
    ],
    {
        "moreResults": "NO_MORE_RESULTS",
        "endCursor": "Cn0Sd2oWc35za2lsZnVsLWZyYW1lLTE4MDIxN3JdCxIFdXNlcnMiJDIxMmU2MTQwLWE1OTQtMTFlNy1iMGQ1LWUxNjBjMjA0NjI2MQwLEgRjYXJzIiQyYjQ5Y2E0MC1hNWZiLTExZTctYWQ1Yy1jOTVhYTc2MTYxYzYMGAAgAA=="
    }
]

1 个答案:

答案 0 :(得分:1)

在这里,您可以执行所需的Node.js代码:

const Datastore = require('@google-cloud/datastore');
const projectId = 'some-projectid'

// Creates a client
const datastore = new Datastore({
  projectId: projectId,
});

const Query = datastore
        .createQuery('cars')
        .filter('plate', '=', 'some-plate-number')

datastore.runQuery(Query).then(results => {
  // Task entities found.
  const tasks = results[0];
  console.log('Tasks:');
  tasks.forEach(task => console.log(task));

输出:

  

任务:{车牌:'some-plate-number',[Symbol(KEY)]:车牌{        命名空间:未定义,        id:“儿童实体ID”,        种类:“汽车”,        父母:         密钥{           命名空间:未定义,           id:“父实体的id”,           种类:“用户”,           路径:[Getter]},        路径:[Getter]}}