0

I have the following data in the store:

{
 properties : 
  [
   {
    _id: 123.
    name: "Nice property"
   },
   {
    _id: 456.
    name: "Another nice property"
   }
 ]
}

In my ngOnInit method I would like to select a property from the store mathcing an id that was passed as a queryParam like this:

javascript
this.id = this.route.snapshot.paramMap.get('id');
this.property = this.ngRedux.select( state => state.properties).pipe(find( property => property._id === this.id))

Obviously, this does not work, but it catches the spirit of what I am trying to do.

Googling selecting by id using ngRedux brings very little, so I have a suspicion that I am doing this the wrong way.

I have tried resolving this using .subscribe, however, then all the logic must be in the subscribe callback, which seems wrong and did not work when I tried to make a reactive form.

Abdulrahman Falyoun
  • 3,676
  • 3
  • 16
  • 43
Andra Zeberga
  • 207
  • 4
  • 14

1 Answers1

0

Try this:

this.property = this.store.select((state) => state.properties)
    .pipe(map(properties => properties.find(x => x_.id == this.id)));

This should work for your given scenario. You simply select properties from the store and then return the first property which matches your id.

ViqMontana
  • 5,090
  • 3
  • 19
  • 54
  • My store is ngRedux and `.pipe` does not exist on it. I need to write `.select` after store and then I can use `.pipe`, but still not having any luck. – Andra Zeberga May 19 '19 at 17:35
  • Have you tried doing .select then piping the map operator to get the property? – ViqMontana May 19 '19 at 18:06
  • Yes @Viqas that turned out the be the right to thing to do. Thank you and sorry for taking so long to get back :) Could you change your answer to reflect that so I can mark it as the right answer? – Andra Zeberga May 20 '19 at 19:03