在猫鼬钩子中调用函数'post save'返回相同的文档

时间:2019-05-07 06:32:38

标签: express mongoose mongoose-schema

在我的模式中,我有一个类似的帖子保存钩子

DateInfoSchema.post('save', function (doc) {
    var newDoc = helper. getListTimeRate(doc);
    console.log(newDoc);
});

然后我有一个帮助程序模块,因为我有一个计算日期范围的函数     让我们考虑“ doc”的开始日期和结束日期,以便我要计算从开始日期到结束日期的日期范围

 module.exports = {
        getListTimeRate: function (data, yearEndDate) {
        var toDate = new Date(),
            weekName = ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"];

        Date.prototype.addDays = function(days) {
           var dateAdd = new Date(this.valueOf());
           dateAdd.setDate(dateAdd.getDate() + days);
           return dateAdd;
        };

        function getDates(startDate, stopDate) {
            var dateArray = [];
            var currentDate = startDate;
            if (currentDate == undefined){
                while (currentDate <= stopDate) {
                    dateArray.push(currentDate);
                    currentDate = currentDate.addDays(1);
                }
                return dateArray;
            }else {
                var newCurrentDate = new Date(currentDate.toISOString().substr(0,10));
                var newStopDate = new Date(stopDate.toISOString().substr(0,10));
                while (newCurrentDate <= newStopDate) {
                    dateArray.push(newCurrentDate);
                    newCurrentDate = newCurrentDate.addDays(1);
                }
                return dateArray;
            }
        }
        function calcRange(entry) {
            // function get time
            var dateList = [],
            newList = [];
            if (toDate < entry.endDate) {
                // calc from endDate
                newList = getDates(entry.startDate, entry.endDate);
                //status often
                selectDateFromOften();
            } else {
                if (yearEndDate) {
                    newList = getDates(entry.startDate, entry.endDate);
                } else {
                    newList = getDates(entry.startDate, toDate);
                }
                // calc from toDate
                //status often
                selectDateFromOften();
            }
            function getDateTimeAnother() {
                if (entry.hour) {
                    var setHour = entry.hour.getHours(),
                    setMin = entry.hour.getMinutes(),
                    setSecond = entry.hour.getSeconds();
                }
                dateList.forEach(function(entryDate){
                    entryDate.setHours(setHour);
                    entryDate.setMinutes(setMin);
                    entryDate.setSeconds(setSecond);
                });

                entry.rangeDate = dateList;
                var compareUsedDate = [];
                _.forEach(entry.frequencyId, function(value) {
                   compareUsedDate.push(value.dateRated);
                });
                entry.rangeDate = _.differenceBy(entry.rangeDate, compareUsedDate,Math.floor);
                entry.rangeDate = entry.rangeDate.filter(function( element ) {
                    return element !== undefined;
                });
                console.log(dateList);
                if (yearEndDate) {
                    return dateList;
                }
            }
            function selectDateFromOften() {
                switch(entry.oftenStatus) {
                    // Daily
                    case "0":
                        for (var i = 0; i< newList.length; i++) {
                            dateList.push(newList[i]);
                        }
                        getDateTimeAnother();
                        break;
                    //Weekly
                    case "1":
                        for (var i = 0; i< newList.length; i++) {
                            if (i == 0 || i % 7 == 0) {
                                dateList.push(newList[i]);
                            }
                        }
                        getDateTimeAnother();
                        break;
                    //Bi-Weekly
                    case "2":
                        for (var i = 0; i< newList.length; i++) {
                            if (i == 0 || i % 14 == 0) {
                                dateList.push(newList[i]);
                            }
                        }
                        getDateTimeAnother();
                        break;
                    //Monthly
                    case "3":
                        // for (var i = newList.length-1; i >= 0; i--) {
                        for (var i = 0; i< newList.length; i++) {
                            if (i == 0 || i % 28 == 0) {
                                dateList.push(newList[i]);
                            }
                        }
                        getDateTimeAnother();
                        break;
                    //Custom Date
                    case "4":
                        dateList = newList;
                        getDateCustom();
                        break;
                    default:
                }
            }
        }
        if (data.length) {
            _.forEach(data, function(entry) {
               if(entry.startDate != undefined){
                   calcRange(entry);
               }
            });
        } else {
            if(data.startDate != undefined){
                calcRange(data);

            }
        }
        return data;
    }
    };

但是在'newDoc'钩子中没有'rangeDate'字段。谁能帮我解决这个问题?谢谢。

0 个答案:

没有答案