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

前端 未结 3 565
天命终不由人
天命终不由人 2021-02-11 01:51

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:30

    "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

提交回复
热议问题