Date: Validated Method can human readable? [Solved]

Gents,

Odd issue with date. On client, after doing some stuff and getting user info:

let conversation = {
      // other fields...
      content: [],
    };

// do random parsing... then

if (text != null) {
      conversation.content.push({
        date: new Date(),
        text: text,
      });
    }

    userUserCommInsert.call(conversation);

On server:

export const userUserCommInsert = new ValidatedMethod({
  name: 'user.userComm.insert',
  validate: new SimpleSchema({
    // ... other validations
    content: { type: Array },
    'content.$': { type: Object },
    'content.$.date' : { type: Date },
    'content.$.text' : { type: Object, blackbox: true },
  }).validator(),
  run({ <whatever>, content }) {
    if (Meteor.isServer) {
      console.log(content);
    }
  },
});

Here is what is odd:

  1. If on client, i set date: new Date().toUTCString();, this falls foul with the validation: validated-method.js:75 Uncaught Error: Date must be a Date [validation-error]. No hassles, I can work around that…
  2. So now, I set date: new Date() on the client side. This passes validation no issues. So the actual date value is some large number of Unix milliseconds. But now, looking at the console.log(content) on the server, shows up as: I20160810-13:14:19.550(10)? [ { date: Wed Aug 10 2016 13:14:19 GMT+1000 (AEST), I20160810-13:14:19.551(10)? text: { entityMap: {}, blocks: [Object] } } ]

How is this possible? Where is the conversion from Unix style milliseconds -> human readable happening. The plan is to move this to moment.js at some point, but ValidatedMethod is being odd here.

Thanks so much.

Tat

new Date() creates a JavaScript Date object, in which there is a toString() method:

JavaScript calls the toString() method automatically when a date is to be represented as a text value or when a date is referred to in a string concatenation.

So it’s not validated-method, nor is it Meteor, it’s just what JavaScript does.

Thank so much mate - this was quite puzzling for a bit.

Tat

1 Like