的条件我有一个用户数据库是这样的:JS如果未定义
const user = {
subscription: {
plan: 'free_trial',
},
};
我需要用户改变计划之前,要检查一些条件。
const currentDate = new Date();
if (user.subscription.trialExpDate > currentDate) {
// do something
} else {
// trialExpDate is either undefined or <= currentDate
user.subscription.trialExpDate = currentDate;
}
我的问题是,对于某些用户trialExpDate
会undefined
。可以将undefined
与currentDate
对象进行比较吗?或者我需要检查一下是否存在trialExpDate
?
什么是'user.subscription.trialExpDate'?它是一个字符串吗?试试'new Date(user.subscription.trialExpDate)' – Rayon
'user.subscription。trialExpDate'或者是'undefined'或者是'date'对象 – cocacrave
'undefined> new Date();'将永远是'false' ..我会在比较之前测试这个值.. – Rayon