我有一个用户模型,我只是试图通过返回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);
}
}
};
答案 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);
}
};