简体   繁体   English

我无法访问从 Ngrx 实体选择器获取的数据的字段值

[英]I can't access fields values of data fetched from Ngrx entities selector

I'm struggling to access the "firstname" properties of the "prsnl" data fetched from ngrx entities, because the prsnl data as you can see on the screenshot of the redux devtool, is in a format that I'm not used to, with fields like "ids" and "entities"我正在努力访问从ngrx实体获取的“prsnl”数据的“名字”属性,因为您可以在redux devtool的屏幕截图上看到prsnl数据的格式我不习惯,带有“ids”和“entities”等字段

这里

I know that the data's field are inside the "entities" but I can't get like "firstname" from there.我知道数据的字段在“实体”内,但我不能从那里得到“名字”。 This is how I selected prsnl data from the store with the createSelector:这就是我使用 createSelector 从存储中选择 prsnl 数据的方式:

export const selectAllPrsnls = createSelector(
    selectPrsnlState,
    prsnlsState => {
        const allPrsnls = Object.values(prsnlsState)
        return allPrsnls;
    }
);

And then I fetched it like like:然后我像这样取它:

getAllPersnlValues() {
    this.store
    .pipe(select(selectPrsnlState), map((prsnlList) => prsnlList))
    .subscribe(value => this.allprsnl = value.entities);
    console.log("All prsnl", this.allprsnl)
  }

And the console.log("All prsnl", this.allprsnl) yields this result as in console:并且console.log("All prsnl", this.allprsnl)在控制台中产生这个结果:

在此处输入图像描述

But I can't access the fields because each entity in there is attached to an ids, I just want to get prsnl's firstname or fullname without the extra informations of entities.但我无法访问这些字段,因为那里的每个实体都附加到一个 ID,我只想获取 prsnl 的名字或全名,而不需要实体的额外信息。 Thanks you in advance !提前谢谢你!

Both your console and the redux devtools are telling you that this.allprsnl = value.entities is returning you an object of nested objects like this:您的控制台和 redux 开发工具都告诉您this.allprsnl = value.entities正在返回一个 object 嵌套对象,如下所示:

All Prsnl:
{
   ids: [1, 2, 3],
   entities: {
     1: {id:1, firstname: "Some title", fullname: "Some fullname",likes: 
          {count: 1}},
     2: {id:2, firstname: "Some title", fullname: "Some fullname" likes: 
          {count: 1}},
     3: {id:3, firstname: "Some title", fullname: "Some fullname" likes: 
          {count: 1}}
   }
}

As you can see the returned response is an object containing ids which is an array of ids, and entities which is itself and object of objects nested into ids.正如您所看到的,返回的响应是一个 object,其中包含ids ,它是一个 ids 数组,以及实体本身和嵌套在 ids 中的对象的 object。 This is why you can't access directely the properties {id, firstname, fullname, ect..} which are nested inside ids, even by calling .subscribe(value => this.allprsnl = value.entities) you still get object of objects.这就是为什么你不能直接访问嵌套在 id 中的属性 {id, firstname, fullname, ect..},即使调用.subscribe(value => this.allprsnl = value.entities)你仍然会得到 object对象。 So my suggestion for you is to transform the object of objects into array of object.所以我对你的建议是将对象的 object 转换为 object 的数组。 A way of doing that in javascript is by using Objects.entries So your subscriptions should become something pretty much like this:在 javascript 中这样做的一种方法是使用Objects.entries所以你的订阅应该变成这样:

getAllPersnlValues() {
    this.store
    .pipe(select(selectPrsnlState), map((prsnlList) => prsnlList))
    .subscribe(value => this.allprsnl = Objects.entries(value[1]).map((e) => ( e[1]));
    console.log("All prsnl", this.allprsnl)
}

With this approach you console should look something like:使用这种方法,您的控制台应该类似于:

All Prsnl:所有prsnl:

[
   {id:1, firstname: "Some title", fullname: "Some fullname",likes: 
          {count: 1}},
   {id:2, firstname: "Some title", fullname: "Some fullname" likes: 
          {count: 1}},
   {id:3, firstname: "Some title", fullname: "Some fullname" likes: 
          {count: 1}}
]

And then you can just access do this.allprsnl.fullname to get the elements' full name.然后你可以访问 do this.allprsnl.fullname来获取元素的全名。 Note: Ngrx-entities mappes by default to something similar to this:注意:默认情况下,Ngrx-entities 映射到类似于以下内容:

Objects.entries(value 1 ).map((e) => ([e[0]]:e 1 )); Objects.entries(value 1 ).map((e) => ([e[0]]:e 1 ));

And the value[1] is to say that you only want data inside entities objects which holds the second place in the array. value[1]是说你只想要在数组中保持第二位的实体对象内的数据。

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

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