angular - 服务中的更新函数每次被调用两次
问题描述
我有一个 Angular 应用程序,在其中我设置了 ngrx 数据,但我注意到每次调用实体服务时
IE
this.purchaseOrderEntityService.update(updatedPurchaseOrder);
它似乎运行了两次并两次访问 http 端点以达到这种效果,似乎无法弄清楚为什么
这是电话之一
if (this.editItem) {
this.subs.sink = this.purchaseService
.updatePurchaseOrderItem(newPoItem)
.subscribe((res: IPurchaseOrderItems) => {
this.itemsService.updateItem(this.purchaseOrderItems, (u) => u.id === res.id, res);
this.settingsService.addinfo("Updated");
this.closeEditor(sender);
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
});
} else {
newPoItem.purchaseOrderId = this.purchaseOrder?.id;
this.subs.sink = this.purchaseService
.addPurchaseOrderItem(newPoItem)
.subscribe((res: IPurchaseOrderItems) => {
this.itemsService.addItemToStart(this.purchaseOrderItems, res);
this.settingsService.addinfo("Added");
this.closeEditor(sender);
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
});
}
当我们删除一个项目时也调用它
this.subs.sink = this.updatePurchaseOrderPrices().subscribe();
完整的更新功能:
updatePurchaseOrderPrices(): Observable<IPurchaseOrder> {
return this.purchaseOrderEntityService.entities$.pipe(
map((po) => po.find((p) => p.id == this.purchaseOrder.id)),
tap((order) => {
const totalNetPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.netAmount, 0); // total amount of the purchase order items including net price
const totalItemPrice = this.purchaseOrderItems.reduce((pv, cv) => pv + cv.totalAmount, 0); // total amount of the purchase order including tax
const totalTaxAmount = totalItemPrice - totalNetPrice;
const updPo: Partial<IPurchaseOrder> = {
id: order.id,
netAmount: totalNetPrice, // should be net item price
taxAmount: totalTaxAmount,
totalAmount: totalItemPrice, //* 0.2,
};
const updatePurchaseOrder: IPurchaseOrder = {
...order,
...updPo,
};
console.log(updatePurchaseOrder);
this.purchaseOrderEntityService.update(updatePurchaseOrder);
}),
first()
);
}
被覆盖的更新函数
update(details: Update<IPurchaseOrder>): Observable<IPurchaseOrder> {
return this.http
.put<IPurchaseOrder>(
`${this.baseUrl}/purchaseorder/poorder/${details.id}`,
{
...details.changes,
}
)
.pipe(
map((res: IPurchaseOrder) => {
return res;
})
);
}
任何想法可能会发生什么?
解决方案
推荐阅读
- arrays - 尝试添加到数组时 C 命令行参数中的分段错误
- kubernetes - 使用 CDK EKS 的私有 Helm 存储库
- c++ - 对于我的 for 循环,我在“=”之前收到错误预期的主表达式
- javascript - 如何访问数组内的对象属性?
- authentication - 有没有办法知道Keycloak的用户账户管理页面是从客户端打开的?
- azure - 无法在 PowerApps 中将自定义 API 添加为自定义连接器
- sql - 根据sql中的字段值乘以字段
- c# - 使用 HttpClient 下载 CSV 文件并使用 CsvHelper onfly 处理
- debugging - 使用 Blazor 调试断点后出现脚本窗口
- php - 如何使用 PHP cURL 使用 OAuth2