0

I've looked quite extensively around for an answer to how to handle my date-related problem, but I can't seem to find a proper answer anywhere.

I'm using SailsJS (beta) with Waterline as the data-handler. My case is as follows:

My User-model is as such:

module.exports = {

  attributes: {

    (.. some attributes ..),
    birthDate: {
      type: 'date',
      required: false
    }

  },

  // Modifies user input before validation
  beforeValidation: function(user, cb){
    // Make sure birthdate is not saved as 0000-00-00
    if(!user.birthDate || user.birthDate == '0000-00-00'){
      user.birthDate == null;
    }
    cb(null, user);
  },
}

The beforeValidation()-function triggers as it should, but I always gets thrown an error as follows. This seems to be the case for both date and datetime types in Waterline models.

warn: Error (E_VALIDATION) :: 1 attribute is invalid
at WLValidationError.WLError (C:\web\node_modules\sails\node_modules\waterline\lib\waterline\error\WLError.js:33:18)
at new WLValidationError (C:\web\node_modules\sails\node_modules\waterline\lib\waterline\error\WLValidationError.js:20:28)
at C:\web\node_modules\sails\node_modules\waterline\lib\waterline\query\validate.js:44:43
at allValidationsChecked (C:\web\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:181:5)
at done (C:\web\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:128:19)
at C:\web\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:25:16
at C:\web\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:162:23
at Object.async.each (C:\web\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:114:20)
at validate (C:\web\node_modules\sails\node_modules\waterline\lib\waterline\core\validations.js:142:11)
at C:\web\node_modules\sails\node_modules\waterline\node_modules\async\lib\async.js:118:13

Invalid attributes sent to User:
  birthDate
    `undefined` should be a date (instead of "0000-00-00", which is a string)

How do I set the birthDate to null in the database using sailsjs/waterline? I hope someone can help:)

hansmei
  • 660
  • 7
  • 17

1 Answers1

2

change

cb(null, user);

to

cb();

And you have a type-mistake in the if-statement:

user.birthDate == null;

have to be

user.birthDate = null;
mdunisch
  • 3,627
  • 5
  • 25
  • 41