I have a service in which Im casting my http payload to an Observable array, I would like to be able to call a method within that service to act on that observable. In my case I would like to confirm an ID exists in the array of Observable ID's. Ive done some research on stackoverflow and I think its because im mishandling the return of the find operator( as I understand it returns an observable ) .
Service :
@Injectable({
providedIn: 'root'
})
export class RuleFieldsService {
baseUrl: string;
relativeUrl: string;
httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
private rulefieldIdSub = new BehaviorSubject<any[]>([]);
private rulefieldIdStore: { ruleFieldIds: any } = { ruleFieldIds: [] };
rulefieldIds$ = this.rulefieldIdSub.asObservable();
constructor(private readonly http: HttpClient, @Inject(APP_BASE_HREF)
private readonly baseHref: string)
{
this.baseUrl = this.baseHref + environment.baseAPIUrl;
};
getRuleFields() {
this.relativeUrl = 'rulefields';
return this.http.get(this.baseUrl + this.relativeUrl);
}
getList(){
this.relativeUrl = 'rulefields';
this.http.get(this.baseUrl + this.relativeUrl + '/enabledropdown').subscribe(data => {
this.rulefieldIdStore.ruleFieldIds = data;
this.rulefieldIdSub.next(Object.assign({}, this.rulefieldIdStore).ruleFieldIds);
});
}
checkRuleFieldType(ruleFieldId) {
console.log('check method')
this.rulefieldIds$.pipe(find((id: any) => id === ruleFieldId)).subscribe(items => {
console.log("ID is in the list")
});
}
}
Component :
@Component({
selector: 'app-rules-tab-wrapper',
templateUrl: './rules-tab-wrapper.component.html',
styleUrls: ['./rules-tab-wrapper.component.scss']
})
export class RulesTabWrapperComponent implements OnInit {
public selectedIndex;
public ruleFieldIds$: Observable<any[]>;
constructor(private rulesetService: RulesetService,
private rulefieldService: RuleFieldsService) { }
ngOnInit() {
this.rulefieldService.getList();
this.ruleFieldIds$ = this.rulefieldService.rulefieldIds$;
this.rulefieldService.checkRuleFieldType(15);
console.log(this.ruleFieldIds$ )
}
}
Ive looked into the Observable to try and get a better idea of where the error may be, but I cant make actionable sense of the information :
source: BehaviorSubject
closed: false
hasError: false
isStopped: false
observers: Array(1)
0: FindValueSubscriber
closed: false
destination: Subscriber {closed: false, _parentOrParents: null, _subscriptions: Array(1), syncErrorValue: null, syncErrorThrown: false, …}
index: 2
isStopped: false
predicate: (id) => id === ruleFieldId
source: Observable
source: BehaviorSubject {_isScalar: false, observers: Array(1), closed: false, isStopped: false, hasError: false, …}
_isScalar: false
__proto__: Object
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
thisArg: undefined
yieldIndex: false
_parentOrParents: Subscriber
closed: false
destination: SafeSubscriber {closed: false, _parentOrParents: null, _subscriptions: null, syncErrorValue: null, syncErrorThrown: false, …}
isStopped: false
syncErrorThrowable: true
syncErrorThrown: false
syncErrorValue: null
_parentOrParents: null
_subscriptions: [FindValueSubscriber]
__proto__: Subscription
_subscriptions: [SubjectSubscription]
__proto__: Subscriber
length: 1
__proto__: Array(0)
thrownError: null
_isScalar: false
_value:
enableDropdownFor: (6) [15, 42, 55, 65, 67, 69]
Thank you for your insight in advance !