Mongoose: Cast to date failed for value when updating a document

前端 未结 3 754
小鲜肉
小鲜肉 2021-02-11 01:57

I use Mongoose in my project and in one of the schemas I use Date field, like this:

reservationDay: {
        type: Date,
        default: Date.now
}
         


        
3条回答
  •  死守一世寂寞
    2021-02-11 02:38

    "20/11/2014 04:11" is an invalid ISODate format. Somewhere you are converting reservationDate into a string of an invalid format. You need to convert it to a valid ISODate format (month before day):

    new Date("11/20/2014 04:11") // Thu Nov 20 2014 04:11:00 GMT+0100 (CET)
    new Date("2014/11/20 04:11") // Thu Nov 20 2014 04:11:00 GMT+0100 (CET)
    new Date("20/11/2014 04:11") // Invalid Date
    

    for easy manipulation of date formats, I'd recommend using moment.js

提交回复
热议问题