从 switchMap rxjs 中的商店选择器获取价值
Getting value from store selector in switchMap rxjs
其实我有一系列的步骤,在store的效果中使用了switchMap。我想从商店获取价值并在 switchMap 中检索它。我尝试如下,但我无法获得所需的类型值
实际结果:
变种客户:任何
预期结果:
clients 应该是商店中的 getClients 类型。
在代码中,我想在下一行代码中从 withLatestFrom 获取值到 switchMap 。在代码中注释为 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))
}
})
)
我收到如下错误:
"You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Interable".
非常感谢您的帮助。谢谢!
你的代码是错误的,一旦你进入 switchMap 函数,你就不再处于 observable 中,所以这就是为什么你遇到与 withLatestFrom 相关的问题。 SwitchMap 还 return 一个数组并创建一个可观察对象,这意味着您不必使用 of()。
我在评论中所说的看起来像这样:
你需要一个新的效果和一个新的动作(loadOk)。我不知道该操作是否需要参数,因为它有点难以理解。
@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;
}
其实我有一系列的步骤,在store的效果中使用了switchMap。我想从商店获取价值并在 switchMap 中检索它。我尝试如下,但我无法获得所需的类型值
实际结果: 变种客户:任何
预期结果: clients 应该是商店中的 getClients 类型。
在代码中,我想在下一行代码中从 withLatestFrom 获取值到 switchMap 。在代码中注释为 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))
}
})
)
我收到如下错误:
"You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Interable".
非常感谢您的帮助。谢谢!
你的代码是错误的,一旦你进入 switchMap 函数,你就不再处于 observable 中,所以这就是为什么你遇到与 withLatestFrom 相关的问题。 SwitchMap 还 return 一个数组并创建一个可观察对象,这意味着您不必使用 of()。 我在评论中所说的看起来像这样: 你需要一个新的效果和一个新的动作(loadOk)。我不知道该操作是否需要参数,因为它有点难以理解。
@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;
}