Problem with Observable after update app to Angular 7 and RxJS 6.3.3

廉价感情. 提交于 2019-12-24 14:13:24

问题


I updated my app to Angular 7 and RxJS 6.3.3 and I've got a problems during making changes into my code. I really need help with that, because I can't to find solution into google or stack overflow.

My imports:

import { Observable, of } from 'rxjs';
import { map, catchError } from 'rxjs/operators';
import { Injectable, Inject, Optional, InjectionToken } from '@angular/core';
import { Headers, ResponseContentType, Response } from '@angular/http';
import { HttpClient } from '@angular/common/http';

Here is code of my test function:

protected test(response: Response): Observable<MyResponseClass | null> {
    const status = response.status;

    if (status === 200) {
       let result200: any = null;
       ... some code ...
       return of<MyResponseClass | null>(result200);
    }
    return of<MyResponseClass | null>(<any>null);
}

Here is MyResponseClass pseudocode:

class MyResponseClass implements IMyResponseClass {
    property1?: string | undefined;
    property2?: string | undefined;
    property3: boolean;

    ...
}

IMyResponseClass pseudocode:

interface IMyResponseClass {
    property1?: string | undefined;
    property2?: string | undefined;
    property3: boolean;
}

Here is my code of my get() function:

get(): Observable<MyResponseClass | null> {
    let url_ = some_url;

    let options_: any = {
        method: "get",
        headers: new Headers({
            "Content-Type": "application/json",
            "Accept": "application/json"
        })
    };

    return this.http.request(url_, options_).pipe(
        map((response_: any) => { return this.test(response_); })),
        catchError((err: any, caught: Observable<MyResponseClass | null>) => {
            if (caught instanceof Response) {
                try {
                    return this.test(<any>caught);
                } catch (e) {
                    return <MyResponseClass | null><any>Observable.throw(e);
                }
            } else
            return <MyResponseClass | null><any>Observable.throw(caught);
        })
    );
}

I'm still getting this error:

Here is a text version of this error:

Argument of type '(err: any, caught: Observable<MyResponseClass>) => MyResponseClass | Observable<MyResponseClass>' is not assignable to parameter of type '(err: any, caught: Observable<MyResponseClass>) => ObservableInput<MyResponseClass>'.
  Type 'MyResponseClass | Observable<MyResponseClass>' is not assignable to type 'ObservableInput<MyResponseClass>'.
    Type 'MyResponseClass' is not assignable to type 'ObservableInput<MyResponseClass>'.
      Type 'MyResponseClass' is not assignable to type 'Iterable<MyResponseClass>'.
        Property '[Symbol.iterator]' is missing in type 'MyResponseClass'.

What I'm doing wrong? Will you help me?


Here is the previous step which I did before that error. Maybe did I something wrong on previous step?


I made a changes after read all of comments so now the get() function looks like that:

return this.http.request(url_, options_).pipe(
            map((response_: any) => { return this.test(response_); }),
            catchError((err: any, caught: Observable<MyResponseClass | null>) => {
                if (caught instanceof Response) {
                    try {
                        return this.test(<any>caught);
                    } catch (e) {
                        return throwError(e);
                    }
                } else
                    return throwError(caught);
            })
        );

and I'm still getting error:


回答1:


I noticed you're returning an Observable from the test function, when you probably shouldn't.

Errors free example: https://stackblitz.com/edit/angular-zimn8f

Check and let me know.




回答2:


Don't import observable from rxjs-compat, use rxjs

The only good reason to use rxjs-compat is if you have dependencies that still rely on an older version of rxjs.



来源:https://stackoverflow.com/questions/54441514/problem-with-observable-after-update-app-to-angular-7-and-rxjs-6-3-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!