4

I have schema object like below

 schedule: [{
        time: {
            type: Date,
            required: true
        }
    }]

and when I try to post data in postman as

"schedule":[
      {
         "time":"18:00:00"
      }]

I'm getting following error

validation failed: schedule.1.time: Cast to Date failed for value "12:00:00"

if I define type as String, everything is working fine.

I think I'm wrong at defining the type to accept the time. Can any one please help me to better way to define the object type

change need
  • 137
  • 2
  • 6
  • 23

4 Answers4

5

I have a simple question for you, Do you want to store just time(no date component eg. 18:00:00) or as a timestamp(date and time eg. 2019-06-11T06:24:46.642Z)?

If it's just time, then unfortunately you can't store it in date typed field because it accepts either date or a timestamp whereas string type can accept any arbitrary string.

I would either suggest storing 18:00:00 as string then process in however way you want after retrieval or storing a timestamp with field typed as date and use it along with its corresponding date.

Just to explain about the error, it occurs because 18:00:00 is no valid way to represent a date since the field is date typed.

PrivateOmega
  • 2,509
  • 1
  • 17
  • 27
1

As you have noticed, the type of field you have defined does not match the data you want to store in it. If you define it as type: Date, it will only accept valid dates and "18:00:00" is not.

From what I understand you want to do, I think the best option may be to store a valid date object even if you are only going to show or edit in your code the time on the client side.

Another possibility could be to declare three number fields at your model: hours, minutes and seconds.

EDIT:

To store just the time as you are asking for, the model could look like:

schedule: [{
    hours: {
        type: Number, required: true, min: 0, max: 23
    },
    minutes: {
        type: Number, required: true, min: 0, max: 59
    },
    seconds: {
        type: Number, required: true, min: 0, max: 59
    }
}]

I don't know how are you going to treat this data but you could get a string like the one you were trying to store with a function like this:

function timeToString(h, m, s) {
    if (h < 10) h = '0' + h;
    if (m < 10) h = '0' + h;
    if (s < 10) h = '0' + h;
    return h + ':' + m + ':' + s;
}
virgiliogm
  • 946
  • 7
  • 15
  • I want to store only Time and then how to declare hours, minute and second. can you show me piece of code – change need Jun 02 '19 at 10:31
  • In your edited answer, you have mentioned as Hours, Minutes and seconds as individual objects. But actually I'm looking for in single object would be accept the Time – change need Jun 02 '19 at 10:51
  • I don't know what you're expecting... I guess you don't want to store it as a string because in your answer you said it worked, but I understand that it's not a proper solution for you (and I also think it isn't). I've suggested how I think I'd do it but we are talking about personal opinions, not solutions to any kind of error since the original error was already fixed: the bug was caused by inserting a string in a date field – virgiliogm Jun 02 '19 at 11:00
0

Look the message it has telling

time: Cast to Date failed for value

Time and Date are different things

If you need to use the time following works as well

{ 
    "time":"2019-06-12T13:34:00.000"
}
Hamit YILDIRIM
  • 4,224
  • 1
  • 32
  • 35
0

It's possible to create a custom type and store time. But I suggest you store the time as a string. In this case, a string is easy to parse, maintain and retrieve.

Add custom validation like this.

time: {
    type: String,
    validate: {
      isAsync: true,
      validator: function(v, cb) {
        setTimeout(function() {
          var timeRegex = /^(?:2[0-3]|[01][0-9]):[0-5][0-9]:[0-5][0-9]$/;
          var msg = v + ' is not a valid time format!';

          cb(timeRegex.test(v), msg);
        }, 5);
      },

      message: 'Default error message'
    },
    required: [true, 'Time is required']
  }
Himakar
  • 185
  • 12
  • This question is off-topic but may I ask why you're using `isAsync: true` in the validator? – svelandiag Mar 25 '21 at 03:42
  • I pasted this snippet from my codebase. You wont need async or the setTimeOut there. Async was used because setTimeOut was used in the validator. – Himakar Apr 21 '21 at 10:37