Итак, у меня есть функция, которая получает корзину покупок из базы. Это асинхронная функция, которая возвращает обещание>. Функция отлично работает на Firefox и Edge, но она ломает мое приложение на Chrome. Я получаю сообщение об ошибке на консоли. У кого-нибудь была подобная проблема раньше?
Вот ошибка для Chrome
core.js:9110 ERROR TypeError: Cannot destructure property items of 'undefined' or 'null'.
at MapSubscriber.project (shopping-cart.service.ts:16)
at MapSubscriber._next (map.js:29)
at MapSubscriber.next (Subscriber.js:49)
at MapSubscriber._next (map.js:35)
at MapSubscriber.next (Subscriber.js:49)
at angularfire2.js:44
at ZoneDelegate.invoke (zone-evergreen.js:359)
at Object.onInvoke (core.js:34201)
at ZoneDelegate.invoke (zone-evergreen.js:358)
at Zone.run (zone-evergreen.js:124)
Вот моя функция
async getCart(): Promise<Observable<ShoppingCart>> {
let cartId = await this.getOrCreateCartId();
return this.db.object('/shopping-carts/' + cartId).valueChanges()
.pipe(map(({items}) => new ShoppingCart(items)));
}
Вот shopping.cart.ts
import { Product } from './product';
import { ShoppingCartItem } from './shopping-cart-item';
export class ShoppingCart {
items: ShoppingCartItem[] = [];
constructor(private itemsMap: { [productId: string]: ShoppingCartItem }) {
this.itemsMap = itemsMap || {};
for (let productId in itemsMap) {
let item = itemsMap[productId];
this.items.push(new ShoppingCartItem({...item, key: productId}));
}
}
getQuantity(product: Product) {
let item = this.itemsMap[product.key];
return item ? item.quantity : 0;
}
get totalPrice() {
let sum = 0;
for (let productId in this.items)
sum += this.items[productId].totalPrice;
return sum;
}
get totalItemsCount() {
let count = 0;
for (let productId in this.itemsMap)
count += this.itemsMap[productId].quantity;
return count;
}
}
Вот корзина-item.ts
export class ShoppingCartItem{
key: string;
title: string;
imageUrl: string;
price: number;
quantity: number;
constructor(init?: Partial<ShoppingCartItem>){
Object.assign(this, init);
}
get totalPrice(){
return this.price * this.quantity;
}
}