0

So I keep getting this error "TypeError: Cannot read property 'otherDocumentaryEvidence' of undefined". I understand I need to initialise this object somehow but I am unsure of the process of this. My understanding is that the object gets defined during the ngOnInit stage from a template service

ngOnInit(): void {
    this._templateService
        .getTemplateSnapshot(this.template)
        .responseData()
        .subscribe(templateSnapStream => {
            return this.shareCase = templateSnapStream.result;
        })
}

My object looks like this

export class ShareCase {
    shareCaseReason?: string;
    caseDetails?: ICasedeit;
    involvedPersonsAndOrganisations?: IInvolved;
    policeOfficer?: IPolice;
    incidents?: IIncident;
    offences?: IOffence;
    suspect?: ISuspectDetails
    witnessStatements?: IStatements;
    interviewNotes?: IIncident;
    notebookEntries?: INotebook;
    victims?: IVictim;
    witnesses?: IWitness;
    physicalEvidence?: IEvidence;
    otherDocumentaryEvidence?: IDocEvidence;
}

In my component code I initialise both the Share Case object and the otherDocumentaryEvidence object in the constructor like so:

 this.shareCase = new ShareCase();
 this.shareCase.otherDocumentaryEvidence = new IDocEvidence();

But I still get the same error? I am unsure of why this is. The line in my template which calls this object also looks like this

 <div *ngIf='shareCase && shareCase.case.otherDocumentaryEvidence && shareCase.case.otherDocumentaryEvidence.length > 0'> 

Any help would be greatly appreciated!

2 Answers 2

1

You are missing an additional check in your *ngIf

<div *ngIf='shareCase && 
            shareCase.case && 
            shareCase.case.otherDocumentaryEvidence && 
            shareCase.case.otherDocumentaryEvidence.length > 0'> 

You are missing shareCase.case check

Sign up to request clarification or add additional context in comments.

2 Comments

Magic! Thank you so much!
Happy to help you
1

Try adding the question mark for null/undefined check.

<div *ngIf='shareCase && shareCase.case?.otherDocumentaryEvidence && shareCase.case?.otherDocumentaryEvidence.length > 0'> 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.