调用http get后调用http post

时间:2018-05-15 21:00:28

标签: typescript angular-httpclient

我正在使用Angular 4应用程序,我正在尝试在将事务写入db之前检索要在我的事务中存储的货件ID。每个电话都是自己工作的,但是如何确保在发出电话之前货件的价值是通过的呢?以下是我的代码:

//From component
saveTrans(): void {

  //Retrieving Shipment Id
  if (this.newTransaction.VehicleId != '') {
    this._data.getShipmentId(this.newTransaction.VehicleId).subscribe(s => this.newTransaction.ShipmentId = s);
  }

    this.item.Transactions.push(this.newTransaction);

    this._data.saveTransaction(this.item).subscribe(() => this.getInventoryTransactions());

    this.closeTrans();
}


//From data service
public getShipmentId(vehicleNbr: string): Observable<number> {
  return this._http.get(url)
    .catch(error => Observable.throw(error))
    .map(response => response.json());
}

public saveTransaction(item: Inventory): Observable<string> {
  const headers = new Headers({ 'Content-Type': 'application/json' });
  const options = new RequestOptions({ headers: this.jsonHeader });

  return this._http.post(url, JSON.stringify(item), options)
    .catch(error => this.handleError(error))
    .map(response => response.toString())
}

0 个答案:

没有答案