NodeJS GCP 数据存储无法查询

NodeJS GCP Datastore Unable To Query

我正在尝试通过 Datastore 自动生成的 ID 查询用户。

下面的 GQL 查询能够 运行 Datastore Console
select * from User where __key__ = Key(User, 123)

但是使用 Datastore JS Library 的 JS 代码不起作用并返回空列表。

const filterKey = dataStore.key(['User', 123]);

dataStore.runQuery(dataStore.createQuery(relationEntity).filter('__key__', filterKey));

我从您的查询中看到,您在 filter 子句中缺少 comparison operator。除此之外, relationEntity 有什么意义?要模拟您的 GQL 查询,它应该是您要获取的实体的 kind 名称。我进行此查询是为了根据您的 GQL 查询 select * from User where __key__ = Key(User, 123):

获取实体
const { Datastore } = require("@google-cloud/datastore");

const datastore = new Datastore();
//Kind = “User”, ID = 5634161670881280, using [default] namespace

const entKey = datastore.key(["User", 5634161670881280]);
const query = datastore.createQuery("User").filter("__key__", "=", entKey); 

datastore.runQuery(query).then(([entities]) => {
    entities.forEach((entity) => {
        console.log(entity.firstname) // prints “Foo” which is the ‘firstname’ in my entity
    });
}).catch((err) => {
    console.log(err);
});

其他可能的原因是 JS 数量限制,并且在 documentation, it’s mentioned that you would need to wrap your auto-generated ID inside datastore.int(“<YOUR_ID>”) for large numbers. If you’d like to see more sample code, there are a lot of snippets in the Datastore Github repository 中。让我知道这是否有用。