Within my application, I have 2 key service components: PromiseService.service.ts (which manages defer calls and asynchronous calls)
@Injectable()
export class PromiseService {
constructor(private staffservice: StaffService) {}
defercall(asyncCalls, syncCalls) {
return new Promise((resolve, reject) => {
const promises = asyncCalls.map(function(val){return val();});
Promise.all(promises).then(result => {
resolve(result);
});
});
}
}
and StaffService.service.ts (responsible for handling staff information)
@Injectable()
export class StaffService {
constructor(private promises: PromiseService) {}
updateStaffInfo() {
this.promises.defercall(this.updateid, this.updateaddress);
}
updateid() {
}
}
However, I encountered the following error:
Circular dependency detected: StaffService.service.ts->PromiseService.service.ts->StaffService.service.ts
I attempted to alter the Promise Service component by removing certain lines, yet a new error surfaced: Unhandled Promise rejection: Cannot read property 'updateid' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'updateid' of undefined
How can I ensure that the PromiseService recognizes that "this" refers to the StaffService? I've been grappling with this issue for a week now. Appreciate any guidance!
UPDATE: Thank you everyone for your responses. Following your advice, I eliminated the StaffService declaration in PromiseService and updated the TypeScript for StaffService as follows:
@Injectable()
export class StaffService {
staffid: any;
staffaddress: any;
constructor(private promises: PromiseService){}
updateid(id) {
this.staffid = id;
}
updateStaffInfo() {
promises.defercall(this.updateid, this.updateaddress);
}
updateaddress(address) {
this.staffaddress = address;
}
}
Despite these changes, when running the ng serve command, the following errors persist:
Unhandled Promise rejection: Cannot read property 'updateid' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'updateid' of undefined
Unhandled Promise rejection: Cannot read property 'updateaddress' of undefined ; Zone: ; Task: Promise.then ; Value: TypeError: Cannot read property 'updateaddress' of undefined
Any further assistance would be greatly appreciated. Thank you.