在 foreach 中执行第一个可观察订阅结束后结果为空?
Empty result after execute first observable subscribe inside foreach to end?
我正在使用 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里面observable的结果但是this.articlepanier总是空的,她在foreach外面
我在这里看到多个问题
您正在 forEach
内订阅。因此,每个元素都将构成一个单独的订阅。相反,您可以使用 forkJoin
、combineLatest
或 zip
等 RxJS 函数来并行订阅多个可观察对象。
您正在尝试同步访问异步变量 articlepanier
。当您尝试在订阅之外访问它时,它还没有调整。您需要使用 switchMap
.
等 RxJS 运算符使整个流具有反应性
尝试以下方法
import { from, forkJoin } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
from(this.storage.get(TOKEN_KEY)).pipe(
filter(token => !(helper.isTokenExpired(token))),
switchMap(_ => {
return forkJoin(this.cart.map((element, index) => {
const article = new Article();
article.artid = element.artid;
article.artcode = element.artcode;
article.plvqteuv = element.amount; //value of the sales unit quantity
let data = {
pricode: this.pricode,
artcode: element.artcode
};
return this.cardProvider.stockbyarticle(data).pipe(
tap((res: any) => {
if (res.quantity < element.amount) { // change boolean status quantity is valid
article.plvqtyisvalid = false;
}
this.articlepanier.splice(index, 0, article); // method changes the contents of array
console.log(this.articlepanier);
})
)
}))
})
).subscribe(
res => {
if (this.articlepanier.filter(c => (c.plvqtyisvalid == true).length > 0)) {
// recording the card in database
}
}
);
注意我正在使用 RxJS from
函数将从 this.storage.get(TOKEN_KEY)
函数返回的 Promise 转换为可观察对象。最好使用 Promises 或 Observables,而不是将它们结合起来。我还使用 RxJS filter
运算符来检查令牌是否仍然有效。
我正在使用 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里面observable的结果但是this.articlepanier总是空的,她在foreach外面
我在这里看到多个问题
您正在
forEach
内订阅。因此,每个元素都将构成一个单独的订阅。相反,您可以使用forkJoin
、combineLatest
或zip
等 RxJS 函数来并行订阅多个可观察对象。您正在尝试同步访问异步变量
等 RxJS 运算符使整个流具有反应性articlepanier
。当您尝试在订阅之外访问它时,它还没有调整。您需要使用switchMap
.
尝试以下方法
import { from, forkJoin } from 'rxjs';
import { filter, map, switchMap } from 'rxjs/operators';
from(this.storage.get(TOKEN_KEY)).pipe(
filter(token => !(helper.isTokenExpired(token))),
switchMap(_ => {
return forkJoin(this.cart.map((element, index) => {
const article = new Article();
article.artid = element.artid;
article.artcode = element.artcode;
article.plvqteuv = element.amount; //value of the sales unit quantity
let data = {
pricode: this.pricode,
artcode: element.artcode
};
return this.cardProvider.stockbyarticle(data).pipe(
tap((res: any) => {
if (res.quantity < element.amount) { // change boolean status quantity is valid
article.plvqtyisvalid = false;
}
this.articlepanier.splice(index, 0, article); // method changes the contents of array
console.log(this.articlepanier);
})
)
}))
})
).subscribe(
res => {
if (this.articlepanier.filter(c => (c.plvqtyisvalid == true).length > 0)) {
// recording the card in database
}
}
);
注意我正在使用 RxJS from
函数将从 this.storage.get(TOKEN_KEY)
函数返回的 Promise 转换为可观察对象。最好使用 Promises 或 Observables,而不是将它们结合起来。我还使用 RxJS filter
运算符来检查令牌是否仍然有效。