简体   繁体   English

在前端访问Google Cloud Datastore密钥

[英]Access google cloud datastore keys on the frontend

I wrote a get query on the node/express to fetch all the entities of a kind. 我在节点/表达式上编写了一个get查询,以获取一种类型的所有实体。 The get request is successful and i see all the properties returned back as json which i can monitor on the front end angular site. get请求成功,我看到所有属性都以json的形式返回,可以在前端角度站点上对其进行监视。 But i do not see NAME/ID property which is basically the keys associated with the enitities. 但是我看不到NAME / ID属性,它基本上是与实体相关的键。 I need the NAME/ID or keys of the entities to perform a different operation. 我需要实体的名称/ ID或键才能执行其他操作。 How can i extract the keys for accessing on the front end UI/html? 如何提取要在前端UI / html上访问的密钥?

GET query which i used - 获取我使用过的查询-

app.get("/api/venues/", (req, res, next) => {
    const query = datastore
    .createQuery('venue');

    query.run().then((venuesList) => {
    // do something
  })

Results i get back after i run the node get - 运行节点后,我得到的结果是-

[ { property1: '<property-value-1>',
    property2: '<property-value-2>',
    property3: '<property-value-3>',
    property4: '<property-value-4>',    
    [Symbol(KEY)]:
     Key {
       namespace: undefined,
       id: <'id-value'>,
       kind: 'venue',
       path: [Getter] } } ]

I am not able to access or see the 'Key' obtained in the above get results on the front end. 我无法访问或看到上面获得的“密钥”在前端获得结果。

Seems like there are very few contributors for google cloud datastore queries on stack overflow. 似乎在堆栈溢出时很少有Google Cloud Datastore查询的贡献者。 Anyways... i did lot of reserach and came up with the below solution for by question and it is working. 无论如何...我做了很多研究,并提出了以下问题解决方案,它正在工作。

The get request through node will fetch all the entities from datastore and all these entities along with their keys would be visible on the node interface. 通过节点的获取请求将从数据存储中获取所有实体,并且所有这些实体及其键将在节点接口上可见。 However, the keys of the entities would not be visible on the UI(Angular, which i am using) eventough we pass on the same json(with entities) to the UI. 但是,如果我们将相同的json(带有实体)传递给UI,则实体的键在UI(Angular,我正在使用)上不可见。

So i made a modification on my json obtained from datastore. 所以我对从数据存储区获取的json进行了修改。 I extracted the keys of entities seperately and created a new attribute within the same json and passed on this modified json to the UI. 我分别提取了实体的键,并在同一json中创建了一个新属性,然后将此修改后的json传递给UI。 So now, i was able to access the keys of entities through an alias attribute which i created at node. 所以现在,我能够通过我在节点上创建的别名属性来访问实体的键。 Check the modified code below - 检查下面的修改后的代码-

app.get("/api/venues/", (req, res, next) => {
    const query = datastore
    .createQuery('venue');

    query.run().then(([venuesList]) => {
    venuesList.forEach(venue => { venue['venueKey'] = venue[datastore.KEY] });
    //venuesList.forEach(venue => console.log(venue));
    res.status(200).json(
      {
        message: "Request was processed successfully!",
        venues: venuesList
      }
    );
  })
})

I hope this helps others in the community. 我希望这会对社区中的其他人有所帮助。

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

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