RangeError:来自API的空数据的无效时间值



我正在从DB中获取一个项目对象,然后将其提供给react中的状态数据。我在deliveryDate、requestedDate和intakeDate中遇到了问题。有时这些值在DB中为空,在这种情况下,我得到一个无效的时间值错误。我如何为空日期值编码?

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
});
}
});

lol I was dumb…

这个解决了问题:

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),

最新更新