In my model class, I have defined an optional property as follows:
export class Workflow {
constructor(
public id: number,
public started: Date,
public documentId: number,
public document: Document,
public status: WorkflowStatus,
public workflowEvents: WorkflowEvent[],
public startingUserId: number,
public statusValue?: string,
) {}
}
I also have a pipe that converts the status property to a string:
import { Pipe, PipeTransform } from '@angular/core';
import { WorkflowStatus } from '../models/workflowstatus.enum';
@Pipe({ name: "status" })
export class WorkflowStatusPipe implements PipeTransform {
public transform(value: WorkflowStatus): string {
switch (value) {
case WorkflowStatus.ForwardedToApprover: return "TOV";
case WorkflowStatus.ReadedByApprover: return "OLV";
case WorkflowStatus.Commented: return "MEGJ";
case WorkflowStatus.Approved: return "JÓV";
case WorkflowStatus.ForwardedToManager: return "MAN";
}
}
}
Next, there are document objects with workflows. The documents are fetched via an API call:
this.documentRepo.getDocuments().subscribe(documents => {
documents.forEach(function (document) {
document.workflow.statusValue = new WorkflowStatusPipe().transform(document.workflow?.status);
});
this.documents = documents;
});
However, attempting to transform the status value to a string generates an error in the console: 'Cannot set property 'statusValue' of null'.
This is perplexing since the statusValue
property is marked as optional.
What modifications should be made to the code?
EDIT:
This is an example of a document object retrieved from getDocuments:
https://i.sstatic.net/F3JW1.png
Also worth mentioning, assigning a random string to statusValue triggers the same error:
this.documentRepo.getDocuments().subscribe(documents => {
documents.forEach(function (document) {
document.workflow.statusValue = "x";//new WorkflowStatusPipe().transform(document.workflow?.status);
});
this.documents = documents;
});