Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 如何存储创建的POST请求ID值以在其他方法中使用_Angular_Typescript_Rest_Post - Fatal编程技术网

Angular 如何存储创建的POST请求ID值以在其他方法中使用

Angular 如何存储创建的POST请求ID值以在其他方法中使用,angular,typescript,rest,post,Angular,Typescript,Rest,Post,我试图将创建的订单的ID用作我需要在之后创建的OrderLine的OrderId 订单将被创建,如果I console.log在SUBSCRIBE方法中记录orderId,它将显示正确的值 但一旦subscribe函数完成,该值就会恢复为undefined 检查方法有效 async checkout() { // Perfom PayPal or Stripe checkout process this.createOrder(); let alert = await

我试图将创建的订单的ID用作我需要在之后创建的OrderLine的OrderId

订单将被创建,如果I console.log在SUBSCRIBE方法中记录orderId,它将显示正确的值

但一旦subscribe函数完成,该值就会恢复为undefined

检查方法有效

 async checkout() {
    // Perfom PayPal or Stripe checkout process
    this.createOrder();
    let alert = await this.alertCtrl.create({
      header: 'Thanks for your Order!',
      message: 'We will deliver your food as soon as possible',
      buttons: ['OK']
    });
    alert.present().then(() => {
      this.modalCtrl.dismiss();
    });   

    this.cart = []; 
     
    this.cartService.removeCartItemCount(); 
  }
createOrder可以工作(除了存储orderId以供重用)

createOrderLine也可以工作,但我不知道如何将orderId设置为正确的Id

 createOrderLine(id){
    const apiUrlOLine = 'api/orderLine';
    for (let i = 0; i< this.cart.length; i++){
      const orderLineForCreation: OrderLineForCreation = {
        orderIdFk : id, //This isn't updating
      itemQty : this.cart[i].menuItemAmount,
      itemComments : "item comment",
      menuItemIdFk : this.cart[i].menuItemId,
      specialIdFk : 1,
      employeeIdFk : 1,
      }
      

      console.log('orderline not created');

      this.repository.create(apiUrlOLine, orderLineForCreation)
        .subscribe(res => {
          this.createdOrderLineId = res.orderLineId;
          console.log('OrderLineCreated');
        });
    }
  }
createOrderLine(id){
常量apiUrlOLine='api/orderLine';
for(设i=0;i{
this.createdOrderLineId=res.orderLineId;
log('OrderLineCreated');
});
}
}
如果有人能告诉我如何解决此问题,我们将不胜感激。

鉴于您订阅的是可观测的,因此对this.repository.create的订阅是异步的。您无法知道订阅何时完成,在完成之前可能会调用日志

this.createOrderLine(this.createdOrderId) //this does not work  
这里的意思是没有调用该方法,还是this.createdOrderId未定义

如果希望从createOrder返回orderId,则必须从createOrder返回一个Observable,并在签出中订阅该Observable

在createOrder中:

在签出中:


您现有的代码中可能存在一个问题,即正在调用this.createOrderLine,并且在创建完项之前返回createOrder-我不确定这是您想要的。您可以将所有项目添加到一个可观察的数组中,然后进行forkJoin,然后订阅结果。

谢谢,
.pipe
部分解决了我的问题
this.createOrderLine(this.createdOrderId) //this does not work  
return this.repository.create(apiUrlOrder,orderForCreation)
  .pipe(
    tap(res => this.createOrderLine(res.orderId))
    map(res => res.orderId)
  );
this.createOrder().subscribe(orderId => {
  //show alert etc.
});