I am not being able to access the properties defined in my model class from making service.ts by making an object of that class

512 views Asked by At

Here, x is not being able to access the properties of ShoppingCart the error it shows is Property item does not exist on type {} I dont know where is mistake i have made which i am not being able to identified

shopping-cart.service.ts

  async getCart(): Promise<Observable<ShoppingCart>> {
    let cartId = await this.getOrCreateCartId();
    return this.db.object('/shopping-carts/' + cartId)
    .valueChanges()
    .pipe(
      map(x => new ShoppingCart(x.items))

    );
  }


**ShoppingCart.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) {

            //we explicitly map each of the object to shoppingCart object 
            let item = itemsMap[productId];

            this.items.push(new ShoppingCartItem({
                // title: item.title,
                // imageUrl: item.imageUrl,
                // price: item.price,
                ...item,
                key: productId
            }));

        }
    }
1

There are 1 answers

0
Vijaya Nistala On

If you still have that problem then this may help...

async getCart(): Promise<Observable<ShoppingCart>> {
const cartId = await this.getOrCreateCartId();
return this.db.object('/shopping-cart/' + cartId).snapshotChanges()
.pipe(map(x => new ShoppingCart(x.payload.exportVal().items)));
}