0

I am fetching a project object from the DB and then feeding it to the state data in react. I ran into problems with deliveryDate, requestedDate and intakeDate. Sometimes these values are null in the DB, and in those cases, I get a invalid time value error. How do I code for null date values?

RangeError: Invalid Time Value

export class ViewProject extends Component{
    constructor(props){
        super(props);
        console.log('constructor values', props)
        this.state={
            projectName:props.projectName,
            driver:"",
            analyst:"",
            team: "",
            requiresSupportTeam: false,
            supportTeam: "",
            allocationEST:"",
            customer:"",
            ITdept:"",
            size: [],
            BIM: "",
            QA: "",
            status: "Intake",
            notes: "",
            shortDescription: "",
            deliveryDate: new Date(),
            requestedDate: new Date(),
            intakeDate: new Date(),
            rank: 0,
            priority: "",
            pointOfContact: "",
            driverDropdownList: [],
            analystDropdownList: [],
            teamDropdownList: [],
            supportTeamDropdownList: [],
            ITdeptDropdownList: ["a", "b", "c"],
            priorityDropdownList: ["High", "Medium", "Low"],
            statusDropdownList: [],
            sizeDropdownList: [1,2,3]
        }    
    }


    componentDidMount() {
fetch(variables.Project_API_URL + 'view-project-detail',
            {
                method:'POST',
                headers:variables.HeadersConfig,
                body: JSON.stringify(
                    props
                )
            })
            .then((response) => response.json())
            .then(project => {
                console.log(project)
                if (typeof(project) !== 'undefined'){
                    this.setState({ 
                        projectName: project.projectName,
                        driver: project.driver,
                        analyst: project.analyst,
                        team: project.team,
                        requiresSupportTeam: project.requiresSupportTeam,
                        supportTeam: project.supportTeam,
                        allocationEST: project.allocationEST ,
                        customer: project.customer,
                        ITdept: project.ITdept,
                        size: project.size,
                        BIM: project.BIM,
                        QA: project.QA ,
                        status: project.status,
                        notes: project.notes,
                        shortDescription: project.shortDescription,
                        deliveryDate: parseISO(project.deliveryDate),
                        intakeDate: parseISO(project.intakeDate),
                        requestedDate: parseISO(project.requestedDate),
                        rank: project.rank,
                        priority: project.priority,
                        pointOfContact: project.pointOfContact
                    });
                }
            });
Linda G
  • 21
  • 3
  • Does this answer your question? [How do I check for null values in JavaScript?](https://stackoverflow.com/questions/6003884/how-do-i-check-for-null-values-in-javascript) – Ergis Apr 19 '22 at 13:58

1 Answers1

0

lol I was dumb...

this solves the issue:

deliveryDate: project.deliverDate == null ? new Date() : parseISO(project.deliveryDate),
requestedDate: project.requestedDate == null ? new Date() : parseISO(project.requestedDate),
intakeDate: project.intakeDate == null ? new Date() : parseISO(project.intakeDate),
FortyTwo
  • 2,414
  • 3
  • 22
  • 33
Linda G
  • 21
  • 3