gpt4 book ai didi

node.js - beforeCreate 在尝试创建条目时未执行 - Sails JS

转载 作者:太空宇宙 更新时间:2023-11-03 22:46:49 31 4
gpt4 key购买 nike

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

最佳答案

beforeCreate 不是一个属性,而是模型上的一个方法,所以它应该是这样的:

attributes: {
....
},

beforeCreate: {
....
}

关于node.js - beforeCreate 在尝试创建条目时未执行 - Sails JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30422783/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com