[英]variable inside Observable subscription gets empty value
I know that Observables take some time to get data while javascript keeps running the others codes and that is troubling me a lot.我知道 Observables 需要一些时间来获取数据,而 javascript 继续运行其他代码,这让我很困扰。
I have used ngrx in my angular project.我在我的 angular 项目中使用了 ngrx。 Here, I am trying to fetch some data from the store which is working fine.
在这里,我正在尝试从运行良好的商店中获取一些数据。 Then, I convert this data stream into
string[]
which is also working fine.然后,我将此数据 stream 转换为
string[]
,这也可以正常工作。 To use this string[]
me subscribe
to this observable.要使用这个
string[]
,我subscribe
了这个 observable。 And inside subscription I try to assign the value to other values named filterSizeValues
.在订阅中,我尝试将值分配给名为
filterSizeValues
的其他值。
Here, the problem comes.这里,问题来了。 If I
console.log
this filterSizeValues
initially I got and empty array.如果我
console.log
这个filterSizeValues
最初我得到了一个空数组。 When the observable finishes his job filterSizeValues
variable is filled with data.当 observable 完成他的工作时,
filterSizeValues
变量会被数据填充。
But I can not effort filterSizeValues
variable to be empty array initially.但我不能努力
filterSizeValues
变量最初是空数组。 What can I do?我能做些什么?
I have already searched the solution in the internet but nothing is working out.我已经在互联网上搜索了解决方案,但没有任何解决方案。
Help me out please.请帮帮我。 And Many Many Thanks in advance.
非常感谢。
Here is my code;这是我的代码;
this.sizeTargetingStore$.dispatch(SizeTargetingActions.getSizeTargeting({
campaignId: this.campaignId,
lineItemId: this.lineItemId
}));
Here I am accessing the store to get data.在这里,我正在访问商店以获取数据。
this.sizeTargeting$
.pipe(switchMap(sizes=>{
let temporary:string[] = [];
sizes.forEach(eachSize=>{
temporary.push(eachSize.name);
})
this.filterSizeValues$ = of(temporary);
return this.filterSizeValues$;
}))
.subscribe(size_name=>{
this.filters.set('size_name', size_name);
})
Here, I am trying to set
the filter values.在这里,我正在尝试
set
过滤器值。
I also tried this way also.我也尝试过这种方式。
this.sizeTargeting$
.pipe(switchMap(sizes=>{
let temporary:string[] = [];
sizes.forEach(eachSize=>{
temporary.push(eachSize.name);
})
this.filterSizeValues$ = of(temporary);
return this.filterSizeValues$;
}))
.subscribe(size_name=>{
this.filterSizeValues = size_name
})
this.filters.set('size_name', this.filterSizeValues);
But all ways filters set to an empty array.但是所有方式过滤器都设置为空数组。 Anyone can help me out please?
任何人都可以帮助我吗?
Normally when I subscribe to something that I am waiting on to return what I do is I set up a Subject:通常,当我订阅我正在等待返回的内容时,我会设置一个主题:
private componentDestroyed$ = new Subject<void>();
then in the Observable piping and subscription I do it as:然后在 Observable 管道和订阅中,我这样做:
this.sizeTargeting$
.pipe(takeUntil(this.componentDestroyed$))
.subscribe((sizes: YourTypeHere[]) => {
if(sizes) {
//Do what I need to do with my sizes here, populate what I need,
//dispatch any other actions needed.
}
})
From my understanding, you have 2 possibilities, either filter out the empty values or skip the first value.据我了解,您有两种可能性,要么过滤掉空值,要么跳过第一个值。 You can do so with the
filter
and skip
rxjs operator respectively.您可以使用
filter
执行此操作,并分别skip
rxjs运算符。
Also I believe that you are misusing the switchMap
operator, since you are not using asynchronous operations within your switchMap
we can use the map
operator instead, so below I have a simplified version of your code with your 2 options to fix your problem.此外,我认为您在滥用
switchMap
运算符,因为您没有在switchMap
中使用异步操作,我们可以使用map
运算符,所以下面我有您的代码的简化版本和您的 2 个选项来解决您的问题。
Option 1:选项1:
this.sizeTargeting$.pipe(
filter(sizes => sizes.length > 0), // filter out empty array values
map(sizes => sizes.map(size => size.name)) // perform your remap
).subscribe(sizes => {
this.filterSizeValues = size_name; // Only arrays with values will reach this step
});
Option 2:选项 2:
this.sizeTargeting$.pipe(
skip(1), // skip the first value
map(sizes => sizes.map(size => size.name)) // perform your remap
).subscribe(sizes => {
this.filterSizeValues = size_name; // Only arrays with values will reach this step
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.