如何返回不同类型的可观察值

发布于 2025-01-17 13:12:53 字数 1112 浏览 0 评论 0原文

我有一个可观察的方法,它返回一些数据,我需要从另一个可观察的方法订阅它,但我收到错误

public getSessionInfo(): Observable<UserInfo> {
   return this.getUserDetails().subscribe((data: any) => {
        return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })
    });
}

我收到错误,订阅缺少 UserInfo 的 Observable 类型的以下属性,但是代码部分

return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })

的类型与是可观察的 UserInfo 但我无法解决这个问题。 请有人建议解决方案,并记下我无法对 getSessionInfo 方法的类型进行任何更改,因为它是覆盖方法。

提前致谢。

I have a observable method which is return some data I need to subscribe this from another observable method but I am getting error

public getSessionInfo(): Observable<UserInfo> {
   return this.getUserDetails().subscribe((data: any) => {
        return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })
    });
}

I am getting error that subscription is missing the following properties of type Observable of UserInfo, however the code section

return of({
            userId: data.UserID,
            tenantId: data.tenantID,
            launchDarklySecureModeHash: '',
            roles: [''],
            emailId: data.EmailID,
            tenantName: data.name,
            userName: data.UserName,
            pendoJWTToken: '',
            location: '/',
            displayLocation: ''
        })

is of same type which is Observable of UserInfo but I am not able to fix this.
Please someone suggest the solution by noted down that I can't make any change in type of getSessionInfo methis as it is override method.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蓝梦月影 2025-01-24 13:12:53

您返回的是 Subscription,而不是返回 Observable。更新包括 switchMap 的代码片段。

public getSessionInfo(): Observable<UserInfo> {
  return this.getUserDetails().pipe(
    switchMap((data: any) => of({
      userId: data.UserID,
      tenantId: data.tenantID,
      launchDarklySecureModeHash: '',
      roles: [''],
      emailId: data.EmailID,
      tenantName: data.name,
      userName: data.UserName,
      pendoJWTToken: '',
      location: '/',
      displayLocation: ''
    }))
  );
}

You are returning a Subscription instead of returning an Observable. Update the snippet including switchMap.

public getSessionInfo(): Observable<UserInfo> {
  return this.getUserDetails().pipe(
    switchMap((data: any) => of({
      userId: data.UserID,
      tenantId: data.tenantID,
      launchDarklySecureModeHash: '',
      roles: [''],
      emailId: data.EmailID,
      tenantName: data.name,
      userName: data.UserName,
      pendoJWTToken: '',
      location: '/',
      displayLocation: ''
    }))
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文