angular - 不能在角度 5 中使用 res.state
问题描述
我有这样的问题。我正在使用角度和节点 js 制作一个 Web 应用程序。在后端,我发送这样的响应。
User.saveUser(newUser, function (err,user) {
if(!err){
res.json({state:true, msg:"data Inserted"});
}
else{
res.json({state:false, msg:"data Is Not Inserted"});
}
});
在前端,我在component.ts文件中做这种事情。
this.userService.registerUser(user).subscribe(res=>{
if(res.state) {
this.flashMessage.show('You are succesfully registerded', {cssClass: 'alert-success', delay: 1000});
}
else{
this.flashMessage.show('registration went wrong', {cssClass: 'alert-danger', delay: 1000});
}
});
在服务文件中,我只是返回这样的响应。
registerUser(user: User){
let headers = new HttpHeaders();
headers.append('Content-Type', 'application/json');
return this.http.post('http://localhost:3000/user/register', user, {headers: headers});
}
当我在组件文件中控制台记录响应时,它会给我正确的输出。但是当我尝试访问 res.state 就像我在上面提供的代码中一样时,它给了我一个错误,说
Property 'state' does not exist on type 'Object'.
有人可以帮我解决这个问题吗?谢谢你!
解决方案
改成如下就行了
this.userService.registerUser(user).subscribe((res:any)=>{
推荐阅读
- r - 在每个值更改时增加一天
- c - 作为 WSARecvFrom 调用的一部分,如何将缓冲区放入 CompletionROUTINE?
- php - 如何在 Fancybox 上添加下载按钮?
- docker - 托管 Linux 预览代理上的缓存 Docker 映像
- sql - How to easily see if many tables are mutually exclusive in SQL?
- c# - 如何防止 C# WPF 中的 UI 挂起?
- sql - 在 select join 语句上添加自动递增列
- c# - 您可以直接使用“CALL Proc_name()”作为字符串,或者在 C# 中使用 CommandType.StoredProcedure
- c# - 为什么 SetSystemTime() 在下午的行为不同?
- jhipster - Using multiple field for combo & list