将应用程序更新到 Angular 7 和 RxJS 6.3.3 后 Observable 的问题 [英] Problem with Observable after update app to Angular 7 and RxJS 6.3.3

查看:19
本文介绍了将应用程序更新到 Angular 7 和 RxJS 6.3.3 后 Observable 的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将我的应用程序更新到了 Angular 7 和 RxJS 6.3.3,但在更改我的代码时遇到了问题.我真的需要这方面的帮助,因为我找不到谷歌或堆栈溢出的解决方案.

我的进口:

import { Observable, of } from 'rxjs';从'rxjs/operators'导入{地图,catchError};从'@angular/core'导入{可注入,注入,可选,注入令牌};从@angular/http"导入{标题,响应内容类型,响应};从'@angular/common/http' 导入 { HttpClient };

这是我的 test 函数的代码:

protected test(response: Response): Observable{const status = response.status;如果(状态 === 200){让 result200: any = null;......一些代码......(result200);}(

这是此错误的文本版本:

 '(err: any, catch: Observable) 类型的参数 =>我的响应类 |Observable'不可分配给类型为(错误:任何,已捕获:Observable<MyResponseClass>)=> 的参数ObservableInput'.输入'MyResponseClass |Observable'不可分配到类型ObservableInput".类型MyResponseClass"不可分配给类型ObservableInput".类型MyResponseClass"不可分配给类型Iterable".MyResponseClass"类型中缺少属性[Symbol.iterator]".

我做错了什么?你会帮我吗?

<小时>

解决方案

我注意到您从 test 函数返回了一个 Observable,而您可能不应该这样做.

无错误示例:https://stackblitz.com/edit/角度zimn8f

检查并告诉我.

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:

解决方案

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.

这篇关于将应用程序更新到 Angular 7 和 RxJS 6.3.3 后 Observable 的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆