订阅的组件无法访问可观察到的响应

问题描述 投票:0回答:1

我的组件已订阅httpService,我正在尝试将来自subscription方法的响应数据存储在cookie /文件存储中。在这里,当我尝试访问来自订阅方法的响应时,收到Property 'data' does not exist on type 'Object'的错误消息。

 Cookie.set('authtoken', response.data.authToken);

但是数据已成功存储在cookie中。

export class SigninComponent implements OnInit {

  public email;
  public password;
 constructor(public http: HttpServiceService, public toastr: ToastrService, public router: Router) { }


 public signInVerify(){    if (!this.email || !this.password) {
      this.toastr.error('Please fill all the mandatory fields')
    }
    else {
      let params = {
        email: this.email,
        password: this.password
      }

      console.log('params')
      console.log(params)
      this.http.getUserSignIn(params)
        .subscribe(response => {
          this.toastr.success('User is logged in Successfully')
          console.log(response.data.authToken)
          Cookie.set('authtoken', response.data.authToken);
          Cookie.set('receiverId', response.data.userDetails.userId);
          Cookie.set('receiverName', response.data.userDetails.firstName + ' ' + response.data.userDetails.lastName);
          this.http.setLocalStorage(response.data.userDetails)

        }
        )
    }
  }

可能是因为它无权访问运行时传入的数据,但是有没有办法处理此问题?

angular observable subscribe httpservice
1个回答
0
投票

尝试将您的回复类型设置为any

 this.http.getUserSignIn(params)
    .subscribe((response: any) => {
     // your code
 })

OR

您可以有一个类似的自定义响应界面

   interface UserSignInResponse{
     data: any;
   }

   this.http.getUserSignIn(params)
        .subscribe((response: UserSignInResponse) => {
         // your code
   })

注意:最好将UserSignInResponse放在其他文件中,在该文件中您还可以放置其他接口并从那里导出此接口。

© www.soinside.com 2019 - 2024. All rights reserved.