Forkjoin 后跟 foreach 导致 'Cannot invoke an expression whose type lacks a call signature.'
Forkjoin followed by foreach causes 'Cannot invoke an expression whose type lacks a call signature.'
尝试在 onInit 函数中订阅 forkJoin 的结果时,出现以下错误:
error TS2349: Cannot invoke an expression whose type lacks a call
signature. Type '((next: (value: Result[]) => void, promiseCtor?:
PromiseConstructorLike) => Promise) | ((next: (value: Account[])
=> void, promiseCtor?: PromiseConstructorLike) => Promise)' has no compatible call signatures.
getAccounts returns Account[] 的可观察对象,getResults returns Result[] 的可观察对象。
当两者都完成加载时,我尝试使用从 getAccounts 函数检索到的帐户的 accessToken 从另一个数据库获取详细的帐户结果,以便我可以将结果添加到详细的帐户中。
简而言之,我正在努力实现这一目标:
let var = getAccountsSimplified();
let var2 = getAllResults();
if both are finished{
foreach(acc in var) {
account = getDetailedAccount(acc.id)
var ownedResults = var2.where(userid === acc.id)
account.results = ownedResults
}
}
这是我到目前为止尝试过的方法,但由于某种原因导致了上述错误。
出于某种原因,console.log(user)
工作正常,这就是为什么我完全迷失了错误。
const tasks$ = [
this.aus.getAccounts(this.auth.token),
this.rs.getResults(this.auth.token)
];
forkJoin(tasks$).subscribe(
value => {
const userList = value[0];
const resultList = value[1];
userList.forEach((acc: Account) => {
this.aus.getUserDetails(acc.access_token).subscribe((user: User) => {
if (user != null) {
console.log(user);
}
});
});
}
);
调用函数的签名:
getAccounts(auth: string): Observable<Array<Account>> {
}
getResults(auth: string): Observable<Array<Result>> {
}
getUserDetails(token: string): Observable<User> {
}
更新
添加 Kurt 的建议后,我现在得到
Argument of type 'MonoTypeOperatorFunction<[Account[], Result[]]>' is
not assignable to parameter of type
'OperatorFunction<(Observable | Observable)[],
[Account[], Result[]]>'. Types of parameters 'source' and 'source'
are incompatible.
Type 'Observable<(Observable | Observable)[]>' is not assignable to type
'Observable<[Account[], Result[]]>'.
Type '(Observable | Observable)[]' is missing the following properties from type '[Account[], Result[]]': 0,
1ts(2345)
好像forkjoin的返回类型有问题
更新 2
原来删除 tasks$ 数组并将函数直接添加到 forkjoin 解决了问题。
尝试将您的嵌套订阅转换为 switchMap
。
我已将您的示例重构为:
- 将
resultList
保存到某个变量或 属性 - 以后可以使用
- 将
userList
传递到 switchMap
- 使用
switchMap
到 forkJoin
对 getUserDetails
的所有调用
- 用户详细信息数组现已到达您的订阅,您可以访问结果列表
const tasks$ = [
this.getAccounts(),
this.getResults()
];
forkJoin(tasks$).pipe(
tap((value: [ Account[], Result[] ]) => this.resultList = value[1]),
map((value: [ Account[], Result[] ]) => value[0]),
switchMap(accounts => forkJoin(
accounts.map(x => this.getUserDetails(x.access_token))
))
).subscribe(details => {
this.userList = details.filter(x => !!x);
console.log(this.resultList);
});
这个例子来自我下面的演示,它演示了这个概念,而不是使用你的确切调用和设置。
尝试在 onInit 函数中订阅 forkJoin 的结果时,出现以下错误:
error TS2349: Cannot invoke an expression whose type lacks a call signature. Type '((next: (value: Result[]) => void, promiseCtor?: PromiseConstructorLike) => Promise) | ((next: (value: Account[]) => void, promiseCtor?: PromiseConstructorLike) => Promise)' has no compatible call signatures.
getAccounts returns Account[] 的可观察对象,getResults returns Result[] 的可观察对象。 当两者都完成加载时,我尝试使用从 getAccounts 函数检索到的帐户的 accessToken 从另一个数据库获取详细的帐户结果,以便我可以将结果添加到详细的帐户中。
简而言之,我正在努力实现这一目标:
let var = getAccountsSimplified();
let var2 = getAllResults();
if both are finished{
foreach(acc in var) {
account = getDetailedAccount(acc.id)
var ownedResults = var2.where(userid === acc.id)
account.results = ownedResults
}
}
这是我到目前为止尝试过的方法,但由于某种原因导致了上述错误。
出于某种原因,console.log(user)
工作正常,这就是为什么我完全迷失了错误。
const tasks$ = [
this.aus.getAccounts(this.auth.token),
this.rs.getResults(this.auth.token)
];
forkJoin(tasks$).subscribe(
value => {
const userList = value[0];
const resultList = value[1];
userList.forEach((acc: Account) => {
this.aus.getUserDetails(acc.access_token).subscribe((user: User) => {
if (user != null) {
console.log(user);
}
});
});
}
);
调用函数的签名:
getAccounts(auth: string): Observable<Array<Account>> {
}
getResults(auth: string): Observable<Array<Result>> {
}
getUserDetails(token: string): Observable<User> {
}
更新
添加 Kurt 的建议后,我现在得到
Argument of type 'MonoTypeOperatorFunction<[Account[], Result[]]>' is not assignable to parameter of type 'OperatorFunction<(Observable | Observable)[], [Account[], Result[]]>'. Types of parameters 'source' and 'source' are incompatible. Type 'Observable<(Observable | Observable)[]>' is not assignable to type 'Observable<[Account[], Result[]]>'. Type '(Observable | Observable)[]' is missing the following properties from type '[Account[], Result[]]': 0, 1ts(2345)
好像forkjoin的返回类型有问题
更新 2
原来删除 tasks$ 数组并将函数直接添加到 forkjoin 解决了问题。
尝试将您的嵌套订阅转换为 switchMap
。
我已将您的示例重构为:
- 将
resultList
保存到某个变量或 属性 - 以后可以使用 - 将
userList
传递到switchMap
- 使用
switchMap
到forkJoin
对getUserDetails
的所有调用
- 用户详细信息数组现已到达您的订阅,您可以访问结果列表
const tasks$ = [
this.getAccounts(),
this.getResults()
];
forkJoin(tasks$).pipe(
tap((value: [ Account[], Result[] ]) => this.resultList = value[1]),
map((value: [ Account[], Result[] ]) => value[0]),
switchMap(accounts => forkJoin(
accounts.map(x => this.getUserDetails(x.access_token))
))
).subscribe(details => {
this.userList = details.filter(x => !!x);
console.log(this.resultList);
});
这个例子来自我下面的演示,它演示了这个概念,而不是使用你的确切调用和设置。