Создание массива дат с помощью пользовательского метода mongoose - PullRequest
0 голосов
/ 10 ноября 2019

Я хочу создать массив дат после того, как пользователь введет дату начала и окончания.

Я создал метод для своей схемы, который будет принимать даты начала и окончания, а затем генерировать массив дат.

Схема

 const monthSchema = new mongoose.Schema({
 startDate: Date,
 endDate: Date,
 dateArray: Array
 });

monthSchema.methods.createDateArray = function(start, end) {
const dateArray = [];
end.setDate(end.getDate() +1);

 while (start < end) {
   let newStart = new Date(start);
   newStart.setDate(newStart.getDate() + 1)

   dateArray.push(newStart);
   start.setDate(start.getDate() + 1)
 }

   return dateArray;
};

const Month = mongoose.model("Month", monthSchema);

Файл EJS

<div class="modal-content">
   <span class="close" >Close &times; </span><br>
   <form action="/addMonth" method="post">
 Date:<br>
 Start Date
<input type="date" required pattern="\y{4}-\d{2}-\d{2}" name="startDate"> 
 <br>
 End Date
 <input type="date" required pattern="\y{4}-\d{2}-\d{2}" name="endDate"> 
 <br>
 <input type="submit">
</form> 

приложение. js

app.post("/addMonth", function(req, res){

  const month = new Month({
  startDate: req.body.startDate,
  endDate: req.body.endDate,
  dateArray: createDateArray(starteDate, endDate)

  });  

   month.save(function(err){
   console.log(month);
   if (!err){
     res.redirect("/admin");
    }
  });
});

Эта строка возвращает ошибку:

 dateArray: createDateArray(starteDate, endDate)

Ошибка:

ReferenceError: createDateArray не определено

Я не уверен, как еще можно создать массив с помощью моего метода после того, как пользователь введет даты. Любая помощь с благодарностью.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...