【问题标题】:ASP .Net Rest Api will not update database entryASP .Net Rest Api 不会更新数据库条目
【发布时间】:2021-06-11 11:18:17
【问题描述】:

情况:

我尝试使用 REST API 更新数据库条目。 REST API 已经过测试并可以使用此代码:

PUT https://[url]/[endpoint]/103 HTTP/1.1
Content-Type: application/json; charset=UTF-8

{
    "cost": 3.49,
    "id": 103,
    "productId": 110,
    "storeId": 5,
    "validFrom": "2020-08-21T00:00:00",
    "validUntil": "0001-01-01 00:00:00"
}

问题:

当我尝试在应用程序中更新它时,它不会更新(也不会返回任何错误)。日志显示该对象已正确传输到 service.ts 中的函数。

这是我正在使用的代码:

组件.ts

let prePriceUpdate = {} as SavePrice;
this.productService.getProduct(result.productId)
    .subscribe((p: Product) => {
        let prePrice = p.prices.find(p => p.store.id == result.storeId) as Price;
        prePriceUpdate.id = prePrice.id;
        prePriceUpdate.productId = result.productId;
        prePriceUpdate.storeId = prePrice.store.id;
        prePriceUpdate.validFrom = prePrice.validFrom;
        prePriceUpdate.validUntil = moment(result.validFrom).format();
        prePriceUpdate.cost = prePrice.cost;
        this.priceService.update(prePriceUpdate);
});

service.ts

  update(price: SavePrice){
    return this.http.put(this.pricesEndpoint + '/' + price.id, price)
      .pipe(map(res => res));
  }

感谢任何提示我犯错的地方。

【问题讨论】:

  • 然后启动调试器并单步调试服务器部分以查找问题

标签: angular typescript rest asp.net-core


【解决方案1】:

日志显示对象已正确传输到函数 在service.ts里面

是的,但是您的服务方法实际上并未发送PUT 请求。那是因为你没有订阅电话。

priceService.update() 调用添加订阅,例如 -

this.productService.getProduct(result.productId)
    .subscribe((p: Product) => {
        // other codes
        this.priceService.update(prePriceUpdate)
            .subscribe(p=> console.log(p));    // subscribe to the call
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    相关资源
    最近更新 更多