Actually, I have series steps and using switchMap in the effect of store. I would like to get value from store and retrieve it in switchMap. I tried as below but I was not able to get desired type value instead
Actual result: var clients: any
Expected results: clients should be of type getClients in the store.
In the code, I want to get value from withLatestFrom to switchMap in the next line of code. Commented in the code as TODO.
@Effect()
loadInResponse$ = this.actions.ofType(fromActions.LOAD_IN).pipe(
withLatestFrom(this.store.select(getNqResponse)),
switchMap(([action, nq]) => {
if (nq.httpStatus === 'OK') {
withLatestFrom(this.store.select(getClients)), //TODO here
switchMap(([action,clients]) => { //TODO here
var i;
var result = [];
console.log(clients);
for (i = 0; i < clients.length; i++) {
result[i] = clients.map(a => a.no);
}
return this.cinReuseService.InCall(result).pipe(
switchMap(responseIn => [
new fromActions.LoadInSuccess(responseIn),
new fromActions.LoadService()
]),
catchError(error => {
let err: responseService = {
status: "FALSE",
message: error
}
return of(new fromActions.LoadInFail(err))
})
)
})
} else {
let err: responseService = {
status: "FALSE",
message: nq.message
}
return of(new fromActions.LoadInFail(err))
}
})
)
I am getting error as below:
"You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Interable".
I would greatly appreciate for the help. Thank you!
Your code is wrong, as soon as you enter the switchMap function, you are no longer in an observable so thats why you had problems related to the withLatestFrom. SwitchMap also return an array and create an observable which means you dont have to use of(). What I said in the comment would look like this: You need a new effect and a new action(loadOk). I dont know if the action need a parameter because its kinda hard to follow.
@Effect()
loadInResponse$ = this.actions.ofType(fromActions.LOAD_IN).pipe(
withLatestFrom(this.store.select(getNqResponse)),
switchMap(([action, nq]) => {
if (nq.httpStatus === 'OK') {
return [new formActions.LoadOk];
} else {
let err: responseService = {
status: "FALSE",
message: nq.message
};
return [new fromActions.LoadInFail(err)];
}
})
@Effect() okResponse = this.actions.ofType(fromActions.LOAD_OK).pipe(
withLatestFrom(this.store.select(getClients)),
switchMap(([action,clients]) => this.cinReuseService.InCall(this.result(clients))),
switchMap(responseIn => [
new fromActions.LoadInSuccess(responseIn),
new fromActions.LoadService()
]),
catchError(error => {
let err: responseService = {
status: "FALSE",
message: error
}
return [new fromActions.LoadInFail(err)];
})
)
result(clients: []): [] {
var i;
var result = [];
for (i = 0; i < clients.length; i++) {
result[i] = clients.map(a => a.no);
}
return result;
}