如何通过生成的 ID 查询实体

How to query entity by generated ID

案例

我对 Google Cloud Datastore 有点陌生,正在从他们的文档和 NodeJS 下的测试中学习。

datastore.save([ entity ], (error, apiResponse) =>
{
    console.log('---', key);
    console.log('---', error);
    console.log('---', apiResponse);
    datastore.get(key).then(([entity]) =>
    {
        console.log('+++', entity);
        console.log('+++', entity[DatastoreClient.KEY] === key);

        let query = datastore.createQuery(kind).filter('__key__', '=', datastore.key(kind));

        datastore.runQuery(query).then(([entity]) =>
        {
            console.log('...', entity);
        }).catch(console.log).then(() =>
        {
            done();
        });
    });
});

控制台中保存的实体输出类似于

{
    a: 'Aa',
    z: 'Zz',
    [Symbol(KEY)]: Key {
        namespace: undefined,
        id: '30',
        kind: 'mch-gw/healthz/jgtdjd6h',
        path: [Getter]
    }
}

问题

当我 运行 查询时,也发生了几行,我得到

Error: 3 INVALID_ARGUMENT: Key path element must not be incomplete: [mch-gw/healthz/jgtdjd6h: ]

我知道我的密钥不必不完整,但如何避免?

最初的目的是什么?

如您所见,上面保存的实体的输出,有一个 id 字段,其值为 '30'

我希望能够通过 id 生成的值进行搜索。

提前感谢大家!!

甚至不需要查询,因为 kindid 是已知的

datastore.save([ entity ], (error, apiResponse) =>
{
    console.log('---', key);
    console.log('---', key.path);
    console.log('---', error);
    console.log('---', apiResponse);

    const id = apiResponse[0].mutationResults[0].key.path[0].id;
    // note however, that if the entity is to be updated: ie it already have
    // entity[DatastoreClient.KEY] as a complete key, then
    // null === apiResponse[0].mutationResults[0].key;

    // also note that this `id` value need to be parsed as int...
    // since it is auto-generated by google-cloud-storage engine
    // and YES, even though output of entity seems to show it as a string
    // it costed me 06h testing à vue, stumbling and messing in order to figure it out, so beware
    const _key = datastore.key([kind, +id]);
    datastore.get(_key).then(([entity]) =>
    {
        console.log('+++', entity);
    }, console.log).then(done);
});

提取输出符合预期

{
    a: 'Aa',
    z: 'Zz',
    [Symbol(KEY)]: Key {
        namespace: undefined,
        id: '42',
        kind: 'mch-gw/healthz/jgtdjd6h',
        path: [Getter]
    }
}