在尝试创建条目时,beforeCreate没有执行 - Sails JS

时间:2015-05-24 11:03:44

标签: node.js sails.js

我有一个用户模型,我只是试图通过返回err来测试beforeCreate,但是当我POST /user时,它会创建模型,而不会执行beforeCreate方法。我在这做错了什么?这是models/User.js

module.exports = {

    connection: "mysql",

  attributes: {

    firstname: {
        type: "string",
        required: true
    },

    lastname: {
        type: "string",
        required: true
    },

    fullname: function() {
        return this.firstname + ' ' + this.lastname;
    },

    username: {
        type: "string",
        required: true
    },

    password: {
        type: "string",
        required: true
    },

    email: {
        type: "email",
        required: true
    },

    status: {
        type: "integer",

        /*
        * 0 - unconfirmed, 1 - confirmed, 2- suspended
        */
        enum: [0, 1, 2]
    },

    // Override toJSON instance method to remove password value
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      return obj;
    },

    // Associations
    roles: {
        collection: "role",
        via: "users"
    },

    permissions: {
        collection: "permission",
        via: "users"
    },

    // Lifecycle Callbacks
      beforeCreate: function(values, next) {
          return next(err); 
    }
  }
};

2 个答案:

答案 0 :(得分:3)

attributes: { .... }, beforeCreate: { .... } 不是属性,而是模型上的方法,所以它应该是这样的:

public abstract class AbstractTestPage {

    public static WebDriver driver;

    public static void goTo(String environment_url){
        driver = Driver.getDriver();
        driver.get(environment_url);
    }

}

public class LoginPage extends AbstractTestPage {

    .
    .
    .
    .
    .
    .
}

答案 1 :(得分:2)

beforeCreate进入attributes。它必须在外面。喜欢 -

module.exports = {

  connection: "mysql",

  attributes: {

    firstname: {
        type: "string",
        required: true
    },

    lastname: {
        type: "string",
        required: true
    },

    fullname: function() {
        return this.firstname + ' ' + this.lastname;
    },

    username: {
        type: "string",
        required: true
    },

    password: {
        type: "string",
        required: true
    },

    email: {
        type: "email",
        required: true
    },

    status: {
        type: "integer",

        /*
        * 0 - unconfirmed, 1 - confirmed, 2- suspended
        */
        enum: [0, 1, 2]
    },

    // Override toJSON instance method to remove password value
    toJSON: function() {
      var obj = this.toObject();
      delete obj.password;
      return obj;
    },

    // Associations
    roles: {
        collection: "role",
        via: "users"
    },

    permissions: {
        collection: "permission",
        via: "users"
    }
  },

  beforeCreate: function(values, next){
    return next(err);
  }
};