Objective: Verify conditional statement after retrieving data from an array
Attempts Made: I explored various articles on SO with similar queries, but they didn't quite match my situation. I need to ensure that the entire Array is populated before evaluating the IF condition.
I experimented with moving the IF condition inside the subscription, however, it resulted in multiple calls.
Debugger Feedback: The process goes smoothly from the getV() call to the method, but upon reaching the first subscribe, it loops back to the IF statement.
Typescript Code:
ngOnInit {
this.getV(vId);
if (this.internalNotes.length > 0) {
this.dialog.open(DialogInternalNotesThreeComponent, {
data: {
data: this.internalNotes
}
});
}
}
public getV(vId: number) {
this.vs.V(vId)
.subscribe(resp => {
this.ps.GetPInternalNotes(resp.PID.toString()).subscribe(respPIN => {
for (let index = 0; index < respPIN.length; index++) {
this.internalNotes.push(respPIN[index]);
}
});
});
}
UPDATE: Placing the IF statement immediately after the FOR loop in the subscribe block seems to prevent duplicate dialogs. Does this implementation appear correct?
public getV(vId: number) {
this.vs.V(vId)
.subscribe(resp => {
this.ps.GetPInternalNotes(resp.PID.toString()).subscribe(respPIN => {
for (let index = 0; index < respPIN.length; index++) {
this.internalNotes.push(respPIN[index]);
}
if (this.internalNotes.length > 0) {
this.dialog.open(DialogInternalNotesThreeComponent, {
data: {
data: this.internalNotes
}
});
}
});
});
}