【发布时间】:2019-07-21 09:09:40
【问题描述】:
我正在尝试通过使用httpclient的get方法来确定文件是否存在。
然后根据结果是否存在文件调用具有不同参数的一种方法。
public faviconLogic(iconName: string): void {
this.getFile(`assets/favicon/${iconName}/favicon.ico`)
.subscribe(
() => this.setFavicon(iconName),
() => this.setFavicon('default')
);
}
private getFile(filename: string): Observable<Blob> {
return this.http.get(filename, { responseType: 'blob' });
}
输入方法 faviconlogic 时出现错误
TypeError: Cannot read property 'length' of undefined
at t.applyUpdate
我尝试添加标题 - 没有。调用方法 getFile 本身可以正常工作,因为订阅返回的 Observable。我做错了什么?
【问题讨论】:
-
applyUpdate是您的代码但未包含在您的问题中吗?你在某个地方给length打电话吗? -
不,我没有在任何地方调用 applyUpdate,也没有调用长度。 applyUpdate 应该是来自 HttpHeaders 的方法。
-
在这种情况下,我们需要一个堆栈闪电战来重现您的错误......
-
@Morlas 你能分享
adding headers的代码吗?该错误表明您正在为标题设置undefined -
@Vadi
public getFile(filename: string, contentType: string): Observable<any> { return this.http.get(filename, { headers: new HttpHeaders({ 'Content-Type': 'image/x-icon' }) }); }
标签: angular observable subscription angular-httpclient