提问人:Krishna Kumar 提问时间:10/10/2023 最后编辑:Krishna Kumar 更新时间:10/10/2023 访问量:58
“PromiseSettledResult<any>”类型的成分股均不可调用
No constituent of type 'PromiseSettledResult<any>' is callable
问:
我在本地构建(ng build)我的项目时出现以下错误。它在本地主机上运行良好。
error TS2349: This expression is not callable.
No constituent of type 'PromiseSettledResult\<any\>' is callable at
results.forEach((result, index) =\>{...})
我的代码是这样的 checkImageUrl 方法,我收到错误
async checkImageUrl(type) {
const myArray = type === 'new' ? this.newArray : this.oldArray;
const promises = myArray.map(item => {
if (!(item?.imageURL.includes('http'))) {
return this.getImage(item?.imageURL)
.then(imageBlob => {
const bolbImageUrl = URL.createObjectURL(imageBlob);
const safeImageUrl = this.sanitizer.bypassSecurityTrustResourceUrl(bolbImageUrl);
return safeImageUrl;
})
.catch(_error => Promise.resolve(item?.imageURL));
} else {
return Promise.resolve(item?.imageURL);
}
});
try {
const results: PromiseSettledResult<any>[] = await Promise.allSettled(promises);
// const results = await Promise.allSettled(promises) as PromiseSettledResult<MyPromiseResult>[];
results.forEach((result, index) => { // -------------> error at this line number
if (result.status === 'fulfilled' && result.value) {
myArray[index].imageURL = result.value;
}
});
} catch (_error) {
this.spinner.hide();
}}
我的getImage方法是:
getImage(docId): Promise<Blob> {
return new Promise((resolve, reject) => {
this.http.get(BaseUrl + '/'+docId, { responseType: 'blob', observe: 'response' })
.subscribe((response: HttpResponse<Blob>) => {
const blob = response.body;
resolve(blob);
}, error => {
reject(error);
});
});
}
我还尝试为此提供结果变量的类型:
const results: PromiseSettledResult\<MyPromiseResult\>\[\] = await Promise.allSettled(promises);
其中 myPromiseResult 被清除为:type MyPromiseResult = SafeResourceUrl | string;
但仍然收到错误
答: 暂无答案
评论