Angular RxJs-获取订户而不是所需的值 [英] Angular RxJs - Getting subscriber instead of the required value

查看:61
本文介绍了Angular RxJs-获取订户而不是所需的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在获得当前客户订单之前,我需要返回他的名字,因此我相关的Service类部分如下所示:

Prior to getting the current customer orders I need to return his name, so my related Service class part looks like the following:

已更新:

export class AuthService {

      customerNameUrl = 'customers/name/' + name;
      . . . 
      getCustomerOrders(customerName: string): Observable<CustomerOrder[]> {
          let currentCustomerName = this.getCurrentCustomer(customerName).subscribe(customer => customer.name);
          console.log(currentCustomerName);     <--- Error
          let customerOrders =  this.http.get<CustomerOrder[]>(this.customerOrdersUrl);
          console.log(customerOrders);
          return customerOrders
      }

      getCurrentCustomer(name: string): Observable<Customer> {
        const url = this.customerNameUrl;
        return this.http.get<Customer>(url).pipe(
          tap(_ => this.log(`fetched customer name=${name}`, 'success')),
          catchError(this.handleError<Customer>(`getCustomer name=${name}`))
        );
      }
      . . .
}

但是第一个console.log显示的是订阅者,而不是所需的值.我试图添加地图运算符以仅从实体获取名称,但没有成功,也许以错误的方式添加了它,知道吗?

But the first console.log shows subscribers instead of the required value. I have tried to add map operator to get only the name from the entity but didn't succeed, maybe added it in the wrong way, any idea?

推荐答案

方法 subscribe 返回 Subscriber .这样说对吗?整个 Observable 和JS本质上都是异步的.您异步获取数据,应该以某种方式等待它,并使用回调继续处理返回的数据.有关此主题的主线程,请参见此处

The method subscribe returns a Subscriber. That makes sense right? The whole Observable and JS by nature, is mainly async. You fetch data async and you should somehow wait for it, and use a callback to continue with the returned data. See here for the main thread about this.

在您的情况下,这意味着您将必须使用某些方法来制作 Observables 链.好东西有很多运算符,必​​须有一个我们可以使用的运算符.在这种情况下,最好的运算符将是 mergeMap concatMap .但是,我不清楚您为什么需要客户名称,因为您没有将其传递给获取客户API.但是,这是否可以解决您的查询?

In your case that would mean that you will have to use something to make the Observables chain. Good thing there are a bunch of operators, there must be one we can use. In this case, the best operator would be mergeMap or concatMap. However, it's unclear to me why you would need the name of the customer, as you are not passing that to the get customer API. Nevertheless, does this solve your query?

getCustomerOrders(customerName: string): Observable<CustomerOrder[]> {
  return this.getCurrentCustomer(customerName).pipe(
    // here you have your customer object, but what do you want to do with it?
    mergeMap((customer) => this.http.get<CustomerOrder[]>(this.customerOrdersUrl))
  );
}

getCurrentCustomer(name: string): Observable<Customer> {
  const url = this.customerNameUrl;

  return this.http.get<Customer>(url).pipe(
    tap(_ => this.log(`fetched customer name=${name}`, 'success')),
    catchError(this.handleError<Customer>(`getCustomer name=${name}`))
  );
}

这篇关于Angular RxJs-获取订户而不是所需的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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