【发布时间】:2020-10-09 06:45:08
【问题描述】:
我正在使用 ionic 3,angular 5, 我不确定我的代码有什么问题,我的结果总是在 foreach 中没有可观察的。这是我的代码:
articlepanier : Article[] = [];
this.storage.get(TOKEN_KEY).then(token => {
let isExpired = helper.isTokenExpired(token);
if(!isExpired){
//foreach loop
this.cart.forEach((element, index) => {
var article = new Article();
article.artid = element.artid;
article.artcode = element.artcode;
//value of the sales unit quantity
article.plvqteuv = element.amount;
let data = {
pricode: this.pricode,
artcode: element.artcode
};
//use observable to return stock available in the database
//and change the value of the article.plvqtyisvalid by false value at each line
//if the quantity entered exceeds the quantity available
this.cardProvider.stockbyarticle(data).subscribe((res: any) => {
if( res.quantity < element.amount ){
//change boolean status quantity is valid
article.plvqtyisvalid = false;
}
//method changes the contents of array
this.articlepanier.splice(index, 0, article);
console.log(this.articlepanier);
});
});
//Here outside of foreach, I want the result of array after executing foreach (this.articlepanier) to be used in a if condition
//But articlepanier is always empty
if(this.articlepanier.filter(c =>(c.plvqtyisvalid == true).length > 0){
//recording the cart in database
}
});
我想要在 foreach 内部观察到的结果,但是 this.articlepanier 总是空的,她在外部 foreach
【问题讨论】:
-
是否进入
stockbyarticle内部?你用控制台日志检查过吗?这将帮助您确切地卡在哪里或从forEach出来。 -
stockbyarticle 是 provider 中的一个函数。是的,我已经在控制台中签入了,但是返回的是空的。