【问题标题】:Typecast multiple responses based on the response from server in angular根据来自服务器的响应以角度类型转换多个响应
【发布时间】:2019-03-22 08:04:22
【问题描述】:
export class LoginResponse {
value: string;
expiration: string;
tokenType: string;
refreshToken: {
    value: string;
    expiration: string;
};
scope: [];
additionalInformation: {};
expiresIn: string;
expired: boolean;

}

export class LoginErrorResponse {
code: number;
error: string;
message: string;
sucess: boolean;

}

return this.httpClient.post(url,body,headers).pipe(map(res=><LoginResponse>res),map(res=><LoginErrorResponse>res));

错误:将“LoginResponse”类型转换为“LoginErrorResponse”类型可能是一个错误,因为这两种类型都没有充分重叠。如果这是故意的,请先将表达式转换为“未知”。 “LoginResponse”类型中缺少属性“code”

我可以从服务器获得任何一个响应。我想根据服务器响应对响应进行类型转换,并且应该返回可观察到的响应。

【问题讨论】:

    标签: angular angular-httpclient rxjs-pipeable-operators


    【解决方案1】:

    您将响应转换为LoginResponse,然后再转换为LoginError,它们是真正不同的接口。所以 LoginResponse 不能是 LoginError 这就是你有这个错误的原因。

    如果登录失败,API 应返回 401,您可以catch the error 并将错误响应转换为您想要的。

    您的 API 无法在同一调用中返回 2 个不同的响应模型 具有相同的状态码。

    【讨论】:

      猜你喜欢
      • 2019-05-02
      • 2011-01-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-11
      • 1970-01-01
      • 2013-08-08
      • 2014-12-18
      相关资源
      最近更新 更多