简体   繁体   English

Angular2 map - > subscribe数据类型错误(类型'ErrorObservable'上不存在属性'length'。)

[英]Angular2 map -> subscribe data type error ( Property 'length' does not exist on type 'ErrorObservable'.)

I have difficulties in web programming. 我在网络编程方面遇到困难。

I want to use HTTP communication, call 'map' and pass the array data. 我想使用HTTP通信,调用'map'并传递数组数据。

makeTables(json:string) {
     let arr:Array<any> = [];
     ...

     return arr;
}

query(spName: string, objParam: Array<any>) {
     return this.http.post(this.url, this.data)
            .map(x => this.makeTables(x.json()));
};

login(username: string, password: string) {
      let ret = this.mysql.query("sp_admin_login", [username, password]);

      ret.subscribe( x => {
           // ERROR !!!!!!!!!
           console.log(x.length);

           if(1 ==1 /*user && user.token*/) {
                 localStorage.setItem("user", "user");
           } else {
           // Q2
                 // ret.mockError(new Error('Username or password is incorrect'));
           }
      });

         return ret;
    }

// ERROR !!!!!!!! //错误!!!!!!!!

The error appears on accessing x.length 访问x.length时出现错误

Error Message is 错误消息是

/admin/src/app/_authentication/authentication.service.ts (17,27): /admin/src/app/_authentication/authentication.service.ts(17,27):

Property 'length' does not exist on type 'any[] | 类型'any [] |上不存在属性'length' ErrorObservable'. ErrorObservable”。
Property 'length' does not exist on type 'ErrorObservable'. 类型'ErrorObservable'上不存在属性'length'。


And a second question: 第二个问题:

How can I call the error method ? 我怎样才能调用错误方法? ( // Q2 ) (// Q2)

ret.mockError(new Error('Username or password is incorrect'));

If you don not mention data type in your callbacks, by default your editor will refer to the return type of the calling method. 如果你没有在回调中提及数据类型,默认情况下你的编辑器会引用调用方法的返回类型。 So query() method can either return Array<any> or can throw error which is of type ErrorObservable . 所以query()方法可以返回Array<any> ,也可以抛出ErrorObservable类型的ErrorObservable If you want to check the length in success call back then you should mention the type specifically. 如果你想检查成功回拨的长度,那么你应该具体提到类型。 ret.subscribe((x : Array<any>) => {});

login(username: string, password: string) {
      let ret = this.mysql.query("sp_admin_login", [username, password]);

      ret.subscribe((x : Array<any>) => {
           // x is a known array now
           console.log(x.length);


           if(1 ==1 /*user && user.token*/) {
                 localStorage.setItem("user", "user");

           } else {
           // Q2
               this.mockError(new Error('Username or password is incorrect'));
           }
      });

         return ret;
    }

Coming to error handling, assuming you already have a error method. 假设您已经有错误方法,即可进行错误处理。 This is not a angular error but its an authentication error since you are calling it when login is not successful. 这不是角度错误,但它是一个身份验证错误,因为您在登录失败时调用它。 Not appropriate to use method like this for handling error cases in applications. 不适合使用这样的方法来处理应用程序中的错误情况。 But still this can be helpful for handling angular errors. 但这仍然有助于处理角度误差。

mockError(error){
  if(error instanceof Error){
    // log error here
    console.log(error);
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 7中的类型&#39;OperatorFunction &lt;{},{}&gt;&#39;错误上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'OperatorFunction<{}, {}>' error in Angular 7 角度2中的类型&#39;void&#39;上不存在属性&#39;subscribe&#39; - Property 'subscribe' does not exist on type 'void' in angular 2 如何解决 Angular 订阅错误? 类型“AngularFireList”上不存在属性“订阅”<unknown> &#39;.ts(2339) - How to solve Angular subscribe error? Property 'subscribe' does not exist on type 'AngularFireList<unknown>'.ts(2339) 类型 {} 上不存在属性“订阅” - Property 'subscribe' does not exist on type {} 属性“地图”在类型“对象”角度上不存在 - Property 'map' does not exist on type 'Object' Angular Angular 2属性订阅在Promise类型上不存在<any> - Angular 2 Property subscribe does not exist on type Promise <any> 在Angular 2 App中返回一个Observable,但仍然出现错误:“ void”类型不存在“属性“ subscribe” - Returning an Observable in Angular 2 App, but still getting error: "Property 'subscribe' does not exist on type 'void' 解决promise angular2:类型“ []”上不存在属性“ includes” - Resolving promise angular2: Property 'includes' does not exist on type '[]' Angular2和Typescript-类型上不存在属性名称 - Angular2 & Typescript - Property name does not exist on type 类型&#39;any []&#39;(JSON)上不存在Angular2属性 - Angular2 Property does not exist on type 'any[]' (JSON)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM