首页 > 解决方案 > 不能在角度 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'.

有人可以帮我解决这个问题吗?谢谢你!

标签: angular

解决方案


改成如下就行了

this.userService.registerUser(user).subscribe((res:any)=>{

推荐阅读