I am dealing with an Angular component where, upon loading, two service methods are called to fetch data in JSON format. My goal is to merge these two sets of data together. After researching other solutions, I discovered that Object.assign
could accomplish this task. However, I encountered an issue - since I am adding data to objects within the subscriber function, the objects are undefined outside of it. Below is my code snippet:
export class UpcomingClassesComponent implements OnInit {
times: ClassTimes = new ClassTimes();
schedule: ClassSchedule = new ClassSchedule();
classes: any;
timing: any;
data: any;
constructor(private router:Router,
private _classService: ClassServiceProxy) {
}
ngOnInit() {
this._classService.GetClassData()
.subscribe((result: any) => {
this.schedule = result;
this.classes = this.schedule;
//console.log(this.classes);
})
this._classService.GetClassTimes()
.subscribe((data: any) => {
this.times = data;
this.timing = this.times;
//console.log(this.timing);
})
let completeData = Object.assign({}, this.classes, this.timing);
console.log(completeData);
}
CompleteData
is returning only an object in the console and nothing else.