首页 > 解决方案 > 订阅不是 Observable 的功能

问题描述

我正在尝试订阅 Observable,但每次尝试订阅时都会出现此错误:

this.authenticateService.getCurrentUser(...).subscribe 不是 new HomeComponent 的函数

我的 getCurrentUser 方法如下所示:

getCurrentUser(): Observable<ICurrentUser> {
        return new Observable<ICurrentUser>(subscriber => {
            from(this.HttpService.get('/user/current')).subscribe(currentUser => {
                subscriber.next(currentUser);
            });
        });
    }

ICurrentUser 接口从 IUser 扩展而来:

    interface IUser {
        uid: string;
        email: string;
        firstname: string;
        name: string;
        language: string;
        companyUid: string;
        companyName: string;
        password?: string;
        newPassword?: string;
        role: string;
        expirationDate: string;
        alternativeEmail?: string;
        alertSubscription?: boolean;
        license?: ILicense;
        isLocked: boolean;
        props: any;
    }
    
    interface ICurrentUser extends IUser {
        timeBeforeExpiration: number;
        graceLoginsRemaining: number;
        usingGraceLogins: boolean;
        autologon: boolean;
        termsAndAgreementsDate: string; // date
        workspaces: number[];
        markets: number[];
        findInAccessibleOnly: boolean;
    }

运行 GET 请求时我从 API 获得的信息:

{
    "uid": "xxxxx-xxxx-xxx-xxx",
    "email": "xxxx@xxxx",
    "firstname": "xxx",
    "name": "xxxx",
    "language": "xx",
    "companyUid": "xxxxx-xxxx-xxxxx-xxxxx",
    "companyName": "xxxx",
    "password": null,
    "newPassword": null,
    "role": "COMPANY_MANAGER",
    "expirationDate": null,
    "alternativeEmail": null,
    "alertSubscription": false,
    "license": null,
    "isLocked": null,
    "props": {
        "ff_findInAccessibleOnly": "true",
        "ff_timer_module": "false",
        "lexprhId": "28936",
        "defaultSearchFilters": "{\"keys\":\"*\",\"suggestReference\":null,\"documentTypeIds\":[],\"attributes\":{\"endDateOfApplicability\":[false],\"findInAccessibleOnly\":[false],\"book\":[],\"type\":[\"101\"],\"nature\":[\"163\",\"105\",\"101\",\"100\",\"103\",\"104\",\"106\",\"107\",\"108\",\"159\",\"158\",\"160\",\"109\",\"157\",\"110\",\"149\",\"115\",\"165\",\"114\",\"116\",\"111\",\"112\",\"113\",\"154\",\"155\",\"156\",\"153\",\"164\",\"117\",\"118\",\"119\",\"121\",\"120\",\"122\",\"124\",\"126\",\"129\",\"125\",\"128\",\"127\",\"130\",\"123\",\"135\",\"145\",\"131\",\"150\",\"134\",\"132\",\"136\",\"133\",\"152\",\"138\",\"151\",\"139\",\"140\",\"141\",\"142\",\"143\",\"144\",\"137\",\"146\",\"147\",\"148\"],\"jurisdiction\":[],\"sourceType\":[],\"source\":[],\"guidelineSource\":[],\"workspace\":[],\"domain\":[],\"territory\":[]},\"pagination\":{\"number\":1,\"size\":30,\"sort\":\"RELEVANCE\"},\"quickSearch\":false}"
    },
    "timeBeforeExpiration": -1,
    "graceLoginsRemaining": -1,
    "usingGraceLogins": false,
    "termsAndAgreementsDate": "2021-06-08T14:48:10.000+00:00",
    "workspaces": [
        1,
        2,
        8,
        101,
        100,
        4,
        102,
        7,
        5,
        3
    ],
    "markets": [
        1,
        0
    ],
    "findInAccessibleOnly": false,
    "autologon": false
}

标签: angulartypescriptrxjsobservablesubscription

解决方案


如果你HttpService正在返回一个承诺,from()并将一个承诺转换为一个可观察的,为什么不直接返回呢?另外,如果getCurrentUser()不需要参数,为什么这是一种方法?我建议如下:

getCurrentUser$ = from(this.HttpService.get<ICurrentUser>('/user/current'));

这使得getCurrentUser$type Observable<ICurrentUser>。任何订阅它的东西都会从 Promise 中获得价值HttpService

注意:我假设该.get()方法允许泛型,否则您可以使用类型断言。


推荐阅读