Within my code, there is a class called WorkspaceDatabase that stems from the Dynamic Tree Example. I have incorporated some debugging information to gain a clearer understanding of the issue at hand.
The Issue:
Upon entering the complete()
function, an unexpected occurrence arises: this.wsnodes.push
results in a
TypeError: Cannot read property 'push' of undefined
, despite my belief that it was properly initialized.
Subsequently, when executing the getChildren
function, a perplexing situation emerges where console.log(this.workspaces);
yields an empty array. However, preceding this, the initialData()
method executes and assigns the value of ws
to this.workspaces
, leading to a correct console output of
(7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}]
.
I would appreciate if someone could shed light on what I might be overlooking here.
export class WorkspaceDatabase {
workspacesService: WorkspacesService;
workspaces: RQMWorkspace[] = new Array();
wsnodes: WorkspaceFlatNode[] = new Array();
setWorkspaceService(workspaceService: WorkspacesService) {
this.workspacesService = workspaceService;
}
constructor() {
this.wsnodes.push(new WorkspaceFlatNode(123, "test", true, 0, true));
console.log("WorkspaceDatabase constructed");
}
initialData() {
this.workspacesService.getWorkspaces().subscribe(
{
next(ws) {
this.workspaces = ws;
console.log(this.workspaces);
},
complete() {
this.workspaces.forEach((ws) => {
if (ws.workspace_id == null) {
console.log(this.wsnodes);
this.wsnodes.push(new WorkspaceFlatNode(ws.id, ws.name, true, 0, true))
}
});
console.log("completed");
console.log(this.wsnodes);
}
}
);
}
getChildren(id: number): WorkspaceFlatNode[] | undefined {
let children: WorkspaceFlatNode[] = new Array();
console.log(this.workspaces);
this.workspaces.forEach((ws) => {
if (ws.workspaceId == id) {
ws.workspaces.forEach((innerWs) => {
children.push(new WorkspaceFlatNode(innerWs.id, innerWs.name, true, 0, true))
});
ws.documents.forEach((doc) => {
children.push(new WorkspaceFlatNode(doc.id, doc.name, false, 0, true))
});
}
})
return children;
}
}