我写了两个函数,如下所示。尽管由于findStartEndDates()返回值上方的控制台日志告诉我这两个值已设置,但由于某种原因,最底层的返回“ startAndEndDates”的“ undefined”。我想念什么?
export function findBillingCycle(day_of_reservation) {
let reservation_date = moment.utc(day_of_reservation).format();
let fifteenth = moment();
fifteenth = fifteenth.startOf('day');
fifteenth = moment.utc(fifteenth);
let fourteenth = moment();
fourteenth = fourteenth.endOf('day');
fourteenth = moment.utc(fourteenth);
fourteenth.date(14);
if (fifteenth.date() < 15) {
fifteenth.subtract(1, 'month');
} else {
fourteenth.add(1, 'month');
}
fifteenth.date(15);
let startAndEndDates = findStartEndDates(reservation_date, fifteenth, fourteenth);
console.log(startAndEndDates);
return startAndEndDates;
}
function findStartEndDates(reservation_date, fifteenth, fourteenth) {
if (moment(reservation_date).isBefore(fifteenth)) {
fifteenth = moment(fifteenth).subtract(1, 'month');
fourteenth = moment(fourteenth).subtract(1, 'month');
}
if (moment(reservation_date).isAfter(fourteenth)) {
fifteenth = moment(fifteenth).add(1, 'month');
fourteenth = moment(fourteenth).add(1, 'month');
}
if (!moment(reservation_date).isBefore(fifteenth) && !moment(reservation_date).isAfter(fourteenth)) {
console.log('returning 15th = ' + moment(fifteenth).format('YYYY-MM-DD'));
console.log('returning 14th = ' + moment(fourteenth).format('YYYY-MM-DD'));
return {
fifteenth: fifteenth,
fourteenth: fourteenth
};
} else {
findStartEndDates(reservation_date, fifteenth, fourteenth);
}
}
您可以将以下内容用作测试数据:
reservation_date: 2018-09-07T13:45:00Z
fifteenth: moment.utc("2018-06-15T05:00:00.000+00:00")
fourteenth: moment.utc("2018-07-14T04:59:59.999+00:00")
更新: 通过在发生Reservation_date的同一个月中创建另外两个日期(第15 /第14个)来找到一种解决方法。这只是简单的逻辑检查,以确定我是否需要回滚或前进一个月。我只是在一个功能中完成所有操作,所以我不需要依赖另一个功能。
答案 0 :(得分:1)
您的问题很可能是由于您的findStartEndDates
函数中的以下几行引起的:
} else {
findStartEndDates(reservation_date, fifteenth, fourteenth);
}
在else
情况下您要进行递归调用,但不会返回此递归调用的结果。这意味着该函数将按预期方式被调用,console.log()
会按预期工作,但是返回值永远不会通过调用堆栈传递,因此您最终会得到undefined
。
您需要将上述代码行更改为此:
} else {
return findStartEndDates(reservation_date, fifteenth, fourteenth);
}
答案 1 :(得分:0)
我认为问题出在您的纳税申报单上,请尝试以下操作:
var values = [fifteenth, fourteenth];
return values;