Reputation: 577
I have a model in mongoose called Object its schema looks like this :
const objectSchema = new Schema(
{
debit: {
type: Float
},
trans_date: {
type: Date
},
{
timestamps: { createdAt: 'created_at', updatedAt: 'last_updated' }
}
);
When I try to save a transaction like :
let obj = {
debit: 100.0,
trans_date: "31/07/2019"
}
await ObjectModel.create(obj)
I am met with the error Cast to Date failed for value "31/07/2019"
Does mongoose not take a date with the format of DD/MM/YYYY, please I need help on this.
Upvotes: 0
Views: 550
Reputation: 61
When I remember correctly mongoose is using the standard javascript constructor for a variable of type Date
. I think your string can not be used for constructing a date. Here is the documentation for Date()
:
Either you have to change your string/date to another format or you just have to use the Date.now() format.
The static Date.now() method returns the number of milliseconds elapsed since January 1, 1970 00:00:00 UTC.
So you will basically just have a long number. Then you can choose the format on the client-side when showing the information to a user.
I hope I could solve your problem.
Upvotes: 1