I've encountered an issue with a business hours object in a mongoose schema that is meant to represent a date. When passing a JSON object and attempting to parse it into a Date as a string, I receive an error message stating: validation failed: business.businessHours.monday.startTime: Cast to date failed for value "08:00" (type string)
A potential workaround could involve using setters to add the current date along with the specified hours and minutes:
date.setHours(08);
date.setMinutes(30);
However, I believe there might be a more efficient solution to this problem. Is there a way to directly pass a date object within a JSON structure?
The JSON structure in question looks like this:
{
...,
"businessHours":{
"monday": {
"startTime": "08:00",
"endTime": "18:30"
},
...
}
}
The corresponding schema appears as follows:
const sellerSchema = new mongoose.Schema({
user: {
type: mongoose.Schema.Types.ObjectId,
ref: "User",
unique: true,
},
...,
business: {
businessHours: {
monday: {
startTime: {
type: Date,
},
endTime: {
type: Date,
},
},
...
},
}
While I understand that conversion prior to saving is possible, I'm still searching for a method to directly parse a string in the format hh:mm to a date. If not, would altering the data representation through a different object type be advised, such as using numbers instead of dates for hours and minutes?
Your insights are much appreciated.