Search code examples
angularngrx

How to get the value from ngrx store


I have an angular 8 project and I am using ngrx to store an array of objects in a state store. I managed to do that, but now, I want to make a post request to the server with the data that is in the state store.

this.dataService.postData(this.store.pipe(select('dataStore')));

That is the code I am trying and it is not working. Thank you


Solution

  • Looks like you are trying to perform side effect. To do that you should do the following in your component/service -

    this.store.pipe(select('dataStore'), 
                    take(1)
                    switchMap((data) => {
                      return this.dataService.postData(data);
                    }
                   ).subscribe(responseOfDataService => {
                      //do whatever you want to do with the response
                      console.log(responseOfDataService);
                   });
    

    BTW - To handle the side effect you should try ngrx Effects - https://ngrx.io/guide/effects