简体   繁体   English

我无法通过创建该类的对象来创建service.ts来访问模型类中定义的属性

[英]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

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 在这里,x不能访问ShoppingCart的属性,它显示的错误是属性项目在类型{}上不存在我不知道我犯了什么错误,我无法识别

shopping-cart.service.ts 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
            }));

        }
    }

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)));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM