Доступ к моим возвращенным данным из вызова метода RxJs Observable - PullRequest
0 голосов
/ 15 октября 2019

(NEWB) Как я могу изменить мой updateUserProfile() Observable метод в моем service, чтобы я мог получить доступ к возвращенным данным? Я хотел бы установить значения моего user в моем service, а не в моем component. Возможно, я могу использовать дополнительную функцию или оператор обратного вызова?

Это мой пользовательский объект:

  user: User = {
    user: {
      first_name: '',
      last_name: '',
      id_number: '',
      email: '',
      password: '',
      phone: '',
      notes: '',
      ricaAddress: {
        street_number: '',
        street_name: '',
        building_name: '',
        floor_level: '',
        unit_number: '',
        suburb: '',
        city: '',
        province: '',
        postal_code: '',
        country: ''
      }
    }
  };

Это мой updateProfile() вызовв моем component:

  updateProfile() {
    this.userService
      .updateUserProfile(this.user)
      .subscribe(
        response => {},
        err => this.handleError(err),
        () => console.log('complete')
      );
  }

Это метод updateUserProfile() в моем сервисе, который я хотел бы изменить :

  updateUserProfile(user): Observable<any> {
    const httpOptions2 = {
      headers: new HttpHeaders({
        'Content-Type': 'application/json',
        apiKey: 'xEr4slh6wdngI-imBb9t8sm4JLY-e1yQz7Tmo7S2A',
        key: '6f4962c7-0706-4cf7-b7ce-0b7dc75fcc57',
        Authorization: this.userToken
      })
    };

    return this.http
      .put(this.apiUrl + 'users' + 'user_id', user, httpOptions2)
      .pipe(catchError(this.handleError));
  }

Спасибовы!

1 Ответ

2 голосов
/ 15 октября 2019

Вы можете добавить трубу map, чтобы вернуть новый, обновленный объект. К вашим услугам:

return this.http
  .put(this.apiUrl + 'users' + 'user_id', user, httpOptions2)
  .pipe(
    map(response => {
      // return a new object from here
    }),
    catchError(this.handleError)
  );

Возвращаемое значение обратного вызова map может быть напрямую доступно в вашем компоненте.

...