Within my application, a server with a rest interface is utilized to manage all database entries. Upon user login, the objective is to load and map all user data from database models to usable models. A key distinction between the two is that database models solely contain ids of child objects, whereas normal models directly inherit their child objects. To achieve this mapping through http requests and incorporate them into the application, the following structure was implemented:
class SomeClass {
private loadProjectData(projectId: string): void {
let s: Sheet;
let d: Dashboard;
this.databaseService.getDocument("Projects", projectId).subscribe(
(project: ProjectDB) => {
this.project = new Project(project.id, project.name, project.theme, []);
for (const dash of project.dashboards) {
this.databaseService
.getDocument("Dashboards", dash)
.subscribe((dashboard: DashboardDB) => {
d = new Dashboard(dashboard.id, dashboard.name, []);
for (const shee of dashboard.sheets) {
this.databaseService
.getDocument("Sheets", shee)
.subscribe((sheet: SheetDB) => {
s = new Sheet(sheet.id, sheet.name, []);
for (const wid of sheet.widgets) {
this.databaseService
.getDocument("Widgets", wid)
.subscribe((widget: WidgetDB) => {
// TODO check widget type
s.widgets.push(
new Widget(
widget.id,
widget.name,
widget.position,
widget.isDeveloped,
widget.type,
),
);
this.dataService.changeProjectData(this.project);
});
}
});
}
d.sheets.push(s);
this.dataService.changeProjectData(this.project);
});
this.project.dashboards.push(d);
this.dataService.changeProjectData(this.project);
}
console.log("Project", this.project);
this.router.navigate(["dashboard"]);
},
err => {
console.log("Error loading project data from database ", err);
},
);
}
}
This code meticulously traverses each hierarchy: Project -> Dashboards -> Sheets -> Widgets. The concept behind this approach is that as each lower-level hierarchical entity is loaded, it's added to the corresponding upper-level parent element. However, upon execution of the code, all objects except for the project turn out to be undefined. Seeking guidance on resolving this issue. Thank you in advance.