gpt4 book ai didi

javascript - 无法迭代接口(interface)。循环变量导致未定义

转载 作者:行者123 更新时间:2023-11-28 03:22:43 27 4
gpt4 key购买 nike

在我的示例中,我创建了一个名为产品的接口(interface)并将数据推送到其中。为此,我创建了一个总函数,它将来自两个源的数据组合起来并推送到接口(interface)对象中。但是当我迭代它时,循环变量变得未定义。在 Total() 函数中,当我迭代 cart 变量时,p 的值未定义。知道为什么吗?

提前谢谢您.ts

  import { stocks } from '../../class-objects/stocks'


export interface Product {

product_id : number;
name : string;
image_url : string;
margin : number;
gst : number;
description : string;
date : Date;
sub_id : number;
unit : string;
quantity : number;
stocks : stocks []

}

export class ItemsPage implements OnInit {

myListObject = [] as Array<Product>

@ViewChild('cart', {static: false, read: ElementRef})fab: ElementRef;

constructor(private cartService: CartService, private modalCtrl: ModalController, public productService : ProductService) { }


ngOnInit() {

this.cartService.getProductDetails().subscribe(data =>{
this.products = data;
this.filter_products = this.products;

console.log(this.products)
})
this.cart = this.cartService.getCart();
this.cartItemCount = this.cartService.getCartItemCount();
}

onChange(quantity,product) { // quantity coming from select from html

this.selected_quant = quantity;
}

addToCart(product) {

this.total(product,this.selected_quant)

}


total(product,quantity) {

console.log(this.cart)

for(let p of this.cart ) {
console.log("Value of p="+p) // resulting in undefined

console.log(p.image_url) // resulting in undefined
console.log(p.name) // resulting in undefined
console.log(product.product_id)

if(p.product_id === product.product_id){
p.quantity = product.quantity
}
else {

this.myListObject.push(product,quantity)
console.log(this.myListObject)
this.cartService.addProduct(this.myListObject);
this.animateCSS('tada');
}
}

}
}

cartservice.ts

  private cart = [];
private cartItemCount = new BehaviorSubject(0);


constructor(private http: HttpClient) { }

getProductDetails (): Observable<any[]> {
return this.http.get<any[]>('http://[::1]:3000/products?&filter[include][][relation]=stocks')

}

getCart() {
return this.cart;
}

getCartItemCount() {
return this.cartItemCount;
}

addProduct(product) {
let added = false;
for (let p of this.cart) {
console.log("cartservice " +p) // resulting in undefined
if (p.product_id === product.product_id) {
p.quantity = product.quantity;

added = true;
break;
}
}
if (!added) {
this.cart.push(product);
}
this.cartItemCount.next(this.cartItemCount.value + 1);
}

最佳答案

替换total函数。

total(product, quantity) {
console.log(this.cartService.getCart());

for (let p of this.cartService.getCart()) {
console.log("Value of p=" + p); // resulting in undefined

console.log(p.image_url); // resulting in undefined
console.log(p.name); // resulting in undefined
console.log(product.product_id);

if (p.product_id === product.product_id) {
p.quantity = product.quantity;
} else {
this.myListObject.push(product, quantity);
console.log(this.myListObject);
this.cartService.addProduct(this.myListObject);
this.animateCSS("tada");
}
}
}

关于javascript - 无法迭代接口(interface)。循环变量导致未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976712/

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