错误TypeError:无法读取未定义的属性“

时间:2019-01-23 11:32:08

标签: http asynchronous service components angular7

我正在开发使用HTTP的Angular 7 App调用Web服务。调用Web服务后,我无法访问组件数据。

我正在使用在用户定义的服务中调用方法的组件。在服务中,我正在发出一个HTTP请求,该请求将在成功响应上调用组件中的函数(作为回调函数)。 但是,出乎意料的是,我的组件中没有函数或变量起作用。

我收到以下错误: 错误TypeError:无法读取未定义的属性“ pushPage”

我是Angular的新手,所以我无法在代码中跟踪问题。

login.component.ts:     SubmitRequest(data:any){

   this.wsHandler.execute(
   {
    type: this.parameterBuilder.MESSAGE_TYPE.LOGIN,
    data: data,
    callback: this.loginResponse,
    loadingMessage: 'signing in'
   });
}

loginResponse (response:any) {
   if(response == null) {
   }
   else if(response.code != 0) {
   }
   else {
      console.log(response);
      this.utils.pushPage(['/dashboard-locations'],null);
   }
}

ws-handler.service.ts:

process() {
        this.sendRequest(parameter, payload.data).subscribe
        ( 
           (response:any) => 
           {                        
              parameter.successCallback(JSON.parse(response._body));               
           },
           (error) => console.log('error is : ' + error),      
           () => console.log('request completed')       
        );
 }
 sendRequest(parameter, data) {
    parameter.successCallback = this.utils.nullToObject(parameter.successCallback, function() {});        
    if(parameter.type == 'GET') {
        return this.http.get(
            parameter.url  
        )
    }
    else if(parameter.type == 'POST') {
        return this.http.post(
            parameter.url,
            parameter.data,
            {headers:headers}
        );
    }
}

}

现在,当我在login.component.ts中调用this.submitRequest时,它将在wsHandler服务中调用一个方法,该服务又在sendRequest方法中调用HTTP方法。现在,在HTTP请求从服务器获得响应之后,作为回调方法,我调用parameter.successCallback(),该方法是login.component.ts中名为loginResponse的方法。虽然,我可以使用console.log语句在函数loginResponse中接收响应,但是当我尝试使用“ this”访问方法/变量时。在下一行执行时,将引发主题行中提到的错误。

任何帮助将不胜感激。预先感谢。

0 个答案:

没有答案
相关问题