gpt4 book ai didi

javascript - Angular 服务调用 API 导致 switchmap 错误

转载 作者:行者123 更新时间:2023-12-02 22:49:07 26 4
gpt4 key购买 nike

我在后端使用 Angular 和 Restful C# API。我正在努力创建一个简单的购物车,并且一整天都在为此苦苦挣扎。当我在页面中调用添加到购物车方法时,逻辑首先检查该商品是否已在购物车中。如果是,它会增加计数并返回乘积,以便更新状态。如果产品不在购物车中,则会将产品添加到购物车中并返回产品对象以更新状态。

最初的调用错误,它把一切搞乱了 - 如果你能帮助我弄清楚发生了什么,我将永远感激不已!

C# 方法

        [HttpGet("GetItem/{cartId}/item/{prodId}")]
public async Task<ProductViewModel> GetItem(string cartId, int prodId)
{
var prod = await _shopCartSvc.GetItem(cartId, prodId);
return prod;
}

[HttpGet("CreateItem/{cartId}/item/{prodId}")]
public async Task<ProductViewModel> CreateItem(string cartId, int prodId)
{
var prod = await _shopCartSvc.CreateItem(cartId, prodId);
return prod; // I stop it here to check my return values
}

在 Angular 形式中,我在产品卡组件中有以下逻辑

   addToCart() {
console.log('addToCart');
this.cartSvc.addToCart(this.product)
.subscribe((p : Product) => {
this.product = p;
console.log('addToCart.done',p);
});
}

这会调用购物车服务 addToCart 公共(public)方法

   addToCart(product: Product) {
console.log('cs.addToCart');
return this.updateItem(product);
}

这又调用私有(private) updateItem 方法来完成所有处理

   private getItem(cartId: string, productId: number) {
console.log('getItem-productId', productId);
return this.http.get(this.getBaseUrl() + 'GetItem/'+ cartId +'/item/' + productId)
.pipe(
map((response: Product) => {
console.log('getItem-done', response);
return response
}),
catchError(this.handleError));
}

private createItem(cartId: string, productId: number) {
console.log('createItem');
return this.http.get(this.getBaseUrl() + 'CreateItem/'+ cartId + '/item/' + productId)
.pipe(
map((response: Product) => {
console.log('createItem-done', response);
return response
}),
catchError(this.handleError));
}

private updateItem(product: Product, amt: number = 1): Observable<Product> {

let cartId = this.getOrCreateCartId();
return this.getItem(cartId, product.id)
.switchMap(item => {
if (item == null) {
console.log('addItem');
// Create new item in cart
this.createItem(cartId, product.id)
.subscribe(item => {
console.log('addItem.done',item);
return of(item);
});
} else {
let total = item['quantity'] + amt;
if (total > 0) {
// Update qty of existing item in cart
item['quantity'] = total;
console.log('Cart Id',cartId,', UpdateItemQty to', total);

this.http.post(this.getBaseUrl() + 'UpdateItem/'+ cartId, item)
.take(1).switchMap( item => {
console.log('UpdateItem.done - newQty', item['quantity']);
return of(item);
});
} else {
// Qty 0 so remove
console.log('DELETE');
this.http.get(this.getBaseUrl() + 'RemoveItem/'+ cartId + '/item/' + item['id']);
return of (product);
} // end if
} // end if
}); // getItem - switchMap

}

在最初将商品添加到购物车期间会发生什么情况,它会抛出错误,但商品仍在添加中。根据日志消息,我得到

console.js:35 cs.addToCart
console.js:35 getItem-productId 1
console.js:35 getItem-done null
console.js:35 addItem
console.js:35 createItem
console.js:35 Global - Unhandled Error: TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
....
console.js:35 createItem-done {name: "Beagle", description: "", imageUrl: "Beagle.png", defaultTextColor: "Black", isPremiumProduct: false, …}
console.js:35 addItem.done {name: "Beagle", description: "", imageUrl: "Beagle.png", defaultTextColor: "Black", isPremiumProduct: false, …}

我是 Angular 的新手,我一生都无法弄清楚问题是什么。我可以进入 C# 后端并在 return 语句上停止它以查看返回的值。这是正确的类型和正确的值(value)观。当我在后端时,错误发生,当我继续流时,它会完成,但由于错误,我的对象状态不会更新。我得到的完整错误是:

console.js:35 Global - Unhandled Error:  TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
at subscribeTo (subscribeTo.js:41)
at subscribeToResult (subscribeToResult.js:11)
at SwitchMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/switchMap.js.SwitchMapSubscriber._innerSub (switchMap.js:51)
at SwitchMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/switchMap.js.SwitchMapSubscriber._next (switchMap.js:41)
at SwitchMapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber._next (Subscriber.js:77)
at CatchSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/map.js.MapSubscriber._next (map.js:41)
at MapSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/operators/filter.js.FilterSubscriber._next (filter.js:38)
at FilterSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at MergeMapSubscriber.push../node_modules/rxjs/_esm5/internal/operators/mergeMap.js.MergeMapSubscriber.notifyNext (mergeMap.js:84)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/InnerSubscriber.js.InnerSubscriber._next (InnerSubscriber.js:15)
at InnerSubscriber.push../node_modules/rxjs/_esm5/internal/Subscriber.js.Subscriber.next (Subscriber.js:54)
at XMLHttpRequest.onLoad (http.js:1534)
at XMLHttpRequest.wrapped (raven.js:376)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:14051)
at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (zone.js:422)
at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (zone.js:195)
at ZoneTask.push../node_modules/zone.js/dist/zone.js.ZoneTask.invokeTask [as invoke] (zone.js:498)
at invokeTask (zone.js:1744)
at XMLHttpRequest.globalZoneAwareCallback (zone.js:1781)

最佳答案

这里:

            if (item == null) {
console.log('addItem');
// Create new item in cart
this.createItem(cartId, product.id)
.subscribe(item => {
console.log('addItem.done',item);
return of(item);
});
}

该项目已创建,因为您有 API 调用和订阅,但没有返回语句。订阅内的内容不算在内。如果你想返回一个流并执行一些副作用,你应该

            if (item == null) {
console.log('addItem');

// Create new item in cart
return this.createItem(cartId, product.id)
.pipe(tap(item => {
console.log('addItem.done',item);
}))
}

关于javascript - Angular 服务调用 API 导致 switchmap 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58259793/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com