Angular 5 HttpClient - 获取,转换和发布,无法使其正常工作

时间:2018-03-14 10:47:12

标签: angular5 angular-httpclient angular-observable

我尝试了各种各样的排列。我想要做的是执行get以便通过Id检索对象,然后修改该对象的属性然后将其发回,以便我有效地更新对象。

这是我尝试过的:

  archive(orgId: number, id: number): Observable<Aircraft> {
    return this.http.get<Aircraft>('/api/organisations/' + orgId + '/aircrafts/' + id)
        .pipe(mergeMap( a => {
          console.log(a);
        return this.http.post<IAircraft>('/api/organisations/' + orgId + '/aircrafts/' + a.id, a).subscribe();
      });
  }

结果:错误类型错误:您提供了一个无效的对象,其中包含一个流。您可以提供Observable,Promise,Array或Iterable。

archive(orgId: number, id: number): Observable<Aircraft> {
    return this.http.get<Aircraft>('/api/organisations/' + orgId + '/aircrafts/' + id)
      .map((data) => {
      return data.current = false;
    })
        .pipe(mergeMap( a => {
        return this.http.post<IAircraft>('/api/organisations/' + orgId + '/aircrafts/' + a.id, a).subscribe();
      }));
  }

错误类型错误:无法创建属性&#39; id&#39;在布尔&#39;假&#39; 和

restore(orgId: number, id: number): Observable<Aircraft> {
    return this.http.get<Aircraft>('/api/organisations/' + orgId + '/aircrafts/' + id)
      .pipe(
        mergeMap( a => {
          a.current = true;
          return this.http.post<IAircraft>('/api/organisations/' + orgId + '/aircrafts/' + a.id, a).subscribe();
        }));
  }

错误类型错误:您提供了一个无效的对象,其中包含一个流。您可以提供Observable,Promise,Array或Iterable。

完整错误:

core.js:1448 ERROR TypeError: You provided an invalid object where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
    at subscribeToResult (subscribeToResult.js:74)
    at MergeMapSubscriber._innerSub (mergeMap.js:138)
    at MergeMapSubscriber._tryNext (mergeMap.js:135)
    at MergeMapSubscriber._next (mergeMap.js:118)
    at MergeMapSubscriber.Subscriber.next (Subscriber.js:92)
    at MapSubscriber._next (map.js:85)
    at MapSubscriber.Subscriber.next (Subscriber.js:92)
    at FilterSubscriber._next (filter.js:90)
    at FilterSubscriber.Subscriber.next (Subscriber.js:92)
    at MergeMapSubscriber.notifyNext (mergeMap.js:151)

我似乎无法使地图正确,如果我将地图取出,它可以正常工作,但那时没有意义。我也尝试将varous map结果转换为observables,但似乎API通过我的诡计看到了,并继续抛出相同的错误。

任何人都可以告诉我这应该如何运作吗?

编辑我继续玩它 - 这是最新的化身:

  archive(orgId: number, id: number): Observable<any> {
    return this.http.get<Aircraft>('/api/organisations/' + orgId + '/aircrafts/' + id)
      .pipe(
        mergeMap(a => {
          console.log(a.constructor.name);
          console.log(a);
           a.current = false;
         return this.http.post<IAircraft>('/api/organisations/' + orgId + '/aircrafts/' a.id, a);
        }));   }

console.log(a.constructor.name)返回&#34; object&#34; - 非常有用

console.log(a)返回

this console output

0 个答案:

没有答案