how to call default blueprint actions in a custom overridden one?

后端 未结 3 1232
情歌与酒
情歌与酒 2021-02-08 04:21

SailsJS provides default blueprint actions, such as find, update, create, etc.

I need to override some of them to suit particular business purposes. However, I would lik

3条回答
  •  眼角桃花
    2021-02-08 05:03

    You should look at lifecycle callbacks in sailsjs. So for example, you can use beforeUpdate or beforeCreate lifecycle callback to do more in the model:

    var bcrypt = require('bcrypt');
    
    module.exports = {
    
      attributes: {
    
        username: {
          type: 'string',
          required: true
        },
    
        password: {
          type: 'string',
          minLength: 6,
          required: true,
          columnName: 'encrypted_password'
        }
    
      },
    
    
      // Lifecycle Callbacks
      beforeCreate: function (values, cb) {
    
        // Encrypt password
        bcrypt.hash(values.password, 10, function(err, hash) {
          if(err) return cb(err);
          values.password = hash;
          //calling cb() with an argument returns an error. Useful for canceling the entire operation if some criteria fails.
          cb();
        });
      }
    };
    

提交回复
热议问题