【发布时间】:2021-07-28 21:53:59
【问题描述】:
我不知道为什么(这是我第一次尝试),但即使我的发布请求正在工作,我也无法在完成后打印任何内容。
在 TS 文件中,这是我的代码:
this.appService.sendPath({ title: serviceToPath }).toPromise().then
(
()=>
{
console.log("The path is set!")
}
);
那么这就是“appService”我的中间人
sendPath(path: any): Observable<any> {
console.log("path from app-service: "+ JSON.stringify(path))
return this.httpClient.post('http://localhost:3000/compare',path)
}
当然是控制台消息“路径已设置!”当“来自应用服务的路径”完美运行时,从不显示在控制台上。
最终目标是在 POST 完成后调用一个函数,但现在,我只想在控制台上打印一些东西。
感谢您的帮助。
更新: 这似乎是因为我的 Post 请求处于永远挂起状态。我该如何解决这个问题?
【问题讨论】:
-
代码看起来正确。你确定执行丰富的代码调用
this.appService.sendPath? -
远程
toPromise(),使用.subscribe并传递错误处理程序以检查是否发生任何错误:this.appService.sendPath({ title: serviceToPath }).subscribe(() => console.log('The path is set!'), (error: any) => console.log('Error setting path:', error)); -
@IAfanasov 这就是为什么确实如此奇怪。是的,它被执行是因为“应用服务的路径:”得到正确显示。
-
@MichaelD 我刚刚尝试了订阅 + 错误但没有弹出错误:
this.appService.sendPath({ title: serviceToPath }).subscribe( res=> { console.log("The path is set!") }, error => { console.log("Sending is wrong " + error) } ); -
非常感谢@IAfanasov 的帮助,非常感谢 :)