I am facing a challenge where I want to use the ID of a newly created Order as the OrderId for an OrderLine that needs to be created immediately after.
After creating the Order, if I log the orderId INSIDE THE SUBSCRIBE METHOD, it shows the correct value.
However, once the subscribe function is completed, the value reverts back to undefined.
The checkout process is functioning correctly.
async checkout() {
// Perform 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();
}
The createOrder function is working fine (except for storing the orderId for future use).
createOrder(){
const orderForCreation: OrderForCreation = {
orderDateCreated: this.currentDate,
orderDateCompleted: this.currentDate,
qrCodeSeatingIdFk: 2
};
const apiUrlOrder = 'api/order';
this.repository.create(apiUrlOrder,orderForCreation)
.subscribe( res =>{
this.createdOrderId = res.orderId;
console.log(this.createdOrderId) //this works
this.createOrderLine(this.createdOrderId) //this does not work
});
console.log(this.createdOrderId); //this does not
}
The createOrderLine function is also working but I am unsure how to set the orderId to the correct 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');
});
}
}
If anyone has suggestions on how to resolve this issue, I would greatly appreciate it.