I have a service set up with some static objects that I am using in my UI.
fetchRulesVariables()
fetchRuleVariables() {
let variables = [
{
name: 'Credit Funding Type',
id: 1,
multiple: false,
hasOperators: true,
availableOperators: this.fetchOperators('inResults'),
inputType: 'single',
placeholder: 'Select a funding type',
availableValues: this.fetchFundingInstraments()
}, {
name: 'Device Trust Score',
id: 2,
multiple: false,
hasOperators: true,
availableOperators: this.fetchOperators('gtltResults'),
inputType: 'text',
placeholder: 'Enter a value between 0 - 100',
availableValues: ''
}
]
return variables;
}
This object runs another function which adds another array of data to it such as availableOperators and availableValues. Those too are just static json objects.
In my component, I have a function that is fetching this data by ID using lodash.
fetchVariableData(id, type) {
let data = [];
switch (type) {
case 'operators':
data = _.find(this.variables, {id}).availableOperators;
break;
}
return data;
}
In my component HTML, I am running an ngFor using my function to fetch the results.
<li *ngFor="let x of fetchVariableData(1, 'operators')"></li>
The issue here is that I am running into some async issues I believe. When I run this, I get an error that availableOperators is undefined. If I hardcode the operators into my fetchVariableData() function however, it works fine.
How can I handle this async issue where it appears that it is trying to use the availableOperators when it isn't ready?
This is all static JSON, no HTTP calls.
Edit1
Component Code:
export class AddRuleComponent implements OnInit {
variables: any;
ngOnInit() {
this.loadVars();
this.renderAddRuleForm();
}
loadVars() {
this.outcomes = this._mserv.fetchOutcomeTypes();
this.variables = this._mserv.fetchRuleVariables();
}
fetchVariableData(id, type) {
let data: any;
switch (type) {
case 'operators':
data = _.find(this.variables, {
id
}).availableOperators;
break;
}
return data;
}
}
Service Code:
fetchRuleVariables() {
let variables = [
{
name: 'Credit Funding Type',
id: 1,
multiple: false,
hasOperators: true,
availableOperators: this.fetchOperators('inResults'),
inputType: 'single',
placeholder: 'Select a funding type',
availableValues: this.fetchFundingInstraments()
}, {
name: 'Device Trust Score',
id: 2,
multiple: false,
hasOperators: true,
availableOperators: this.fetchOperators('gtltResults'),
inputType: 'text',
placeholder: 'Enter a value between 0 - 100',
availableValues: ''
}
]
return variables;
}
fetchOperators(type) {
let operators = [];
// Based on the set of operators we want
switch (type) {
// In Results
case 'inResults':
operators.push({
name: 'In List',
id: 1,
}, {
name: 'Not In List',
id: 2,
});
break;
// Greater than & Less than
case 'gtltResults':
operators.push({
name: 'Greater Than <',
id: 3,
}, {
name: 'Less Than >',
id: 4,
});
break;
// Is / is not
case 'isisnot':
operators.push({
name: 'Is',
id: 5,
}, {
name: 'Is Not',
id: 6,
});
break;
}
return operators;
}
HTML Code:
<select class="form-control input-sm" formControlName="operator" [attr.id]="'operator'+i">
<option value="">Select an Operator</option>
<option *ngFor="let o of fetchVariableData(1, 'operators') | values" value="{{ o.id }}">{{ o.name }}</option>
</select>
Values Pipe:
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'values', pure: false })
export class ValuesPipe implements PipeTransform {
transform(value: any, args: any[] = null): any {
return Object.keys(value).map(key => value[key]);
}
}
let variablesis defined in my service file. In my component, within myngOnInit, I am runningthis.variables = this._mserv.fetchRuleVariables();In my component, I then accessthis.variablesfrom my code mentioned viafetchVariableData(). Does this clear it up any more ?fetchOperatorsimplementation?variables: any = []first, then do an*ngIf="variables.length"on its container. Out of curiosity, what do you get if youconsole.log(id, type)right after the function definition in fetchVariableData, i bet first time might be called without any valid value, therefore the find wont get anything and that's where the undefined comes.