Home > Back-end >  How to handle "ERROR Error: InvalidPipeArgument: 'Unable to convert "Invalid Date&quo
How to handle "ERROR Error: InvalidPipeArgument: 'Unable to convert "Invalid Date&quo

Time:08-24

I'm having a condition where the date-time stamp is sometimes null. In those cases my date pipe is failing.

HTML:

<div *ngIf="studentInfo?.classRoom?.id;">
  {{getDateObject(studentInfo?.classRoom?.dob) | date: sharedModule.getDateTimeFormat() :
              sharedModule.getDateTimeZoneDefault(getDateObject(studentInfo?.classRoom?.dob))}}
</div>

This runs good untill the dateString in below function is passed null.

TS:

getDateObject(dateString) {
  return moment(dateString).toDate();
}

That's when it starts logging:

ERROR Error: InvalidPipeArgument: 'Unable to convert "Invalid Date" into a date' for pipe 'DatePipe'

Can someone please help me with this. Passing null value is still fine. But the UI should handle this Pipe issue. Please help.

CodePudding user response:

You need to add a if condition to handle this, that's all, so that null is not passed to the date pipe!

<div *ngIf="studentInfo?.classRoom?.id;">
  {{studentInfo?.classRoom?.dob ? 
             getDateObject(studentInfo?.classRoom?.dob) | date: sharedModule.getDateTimeFormat() :
              sharedModule.getDateTimeZoneDefault(getDateObject(studentInfo?.classRoom?.dob))
 : ''}}
</div>
  • Related