文章内容
2019/5/6 16:24:58,作 者: 黄兵
A function whose declared type is neither 'void' nor 'any' must return a value.
最近使用Observable的方式请求资源,报如下错误:
A function whose declared type is neither 'void' nor 'any' must return a value.
出现问题的原因:
首先看看代码是如何写的:
getUserIdByEmail(UserId: string): Observable<any>{ const body: string = JSON.stringify(UserId); this.http.post('/api/xxx/xxx', body, { headers: new HttpHeaders().set('Content-Type', 'application/json') }).pipe( map((response: Response) => { return response; }), catchError((error: any) => { return throwError(error); })); }
申明了一个返回类型,但是没有返回任何东西。
解决方案:
修改源代码:
getUserIdByEmail(UserId: string): Observable<any>{ const body: string = JSON.stringify(UserId); return this.http.post('/api/xxx/xxx', body, { headers: new HttpHeaders().set('Content-Type', 'application/json') }).pipe( map((response: Response) => { return response; }), catchError((error: any) => { return throwError(error); })); }
this.http.post(...)返回的是Observable不是一个值。
参考资料:
1、A function whose declared type is neither 'void' nor 'any' must return a value
黄兵个人博客原创。
转载请注明出处:黄兵个人博客 - A function whose declared type is neither 'void' nor 'any' must return a value.
评论列表