模型生命周期 Sails 1 中的助手
Helpers in model life cycle Sails 1
好吧,我做了一个加密密码的助手,我试图在我的用户模型的生命周期中使用它,但是当我调用请求时,它进入了一种循环,它 returns没有回应。
这是我的 action2(创建用户)[路线:'post /user':'user/create']
module.exports = {
friendlyName: 'Create user',
description: 'action create user',
inputs: {
name: {
description: 'user name',
type: 'string',
required: true
},
username: {
description: 'username unique',
type: 'string',
required: true
},
email: {
description: 'user email',
type: 'string',
required: true
},
password: {
description: 'user password',
type: 'string',
required: true
},
nivel: {
description: 'user nivel access (see in polices)',
type: 'string',
required: false
},
status: {
description: 'user status',
type: 'string',
required: false
}
},
exits: {
success: {
description: 'success by default JSON'
},
notFound: {
description: 'not found',
responseType: 'notFound'
}
},
fn: async (inputs, exits) => {
const { name, username, email, password, nivel, status } = inputs;
var user = await User.create({ name, username, email, password, nivel, status }).fetch();
if(!user) return exits.notFound();
return exits.success(user);
}
}
这是我的模型(用户)
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
username: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'string',
unique: true,
required: true,
isEmail: true
},
password: {
type: 'string',
required: true,
minLength: 3
},
passwordResetExpires: {
type: 'string',
},
nivel: {
type: 'string',
isIn: ['developer', 'administrator', 'attendant'],
defaultsTo: 'attendant'
},
status: {
type: 'string',
isIn: ['activated', 'disabled'],
defaultsTo: 'activated'
}
},
customToJSON: () => {
return _.omit(this, ['password', 'passwordResetExpires']);
},
// Lifecycle Callbacks
beforeCreate: (values, cb) => {
//cipher helper
sails.helpers.cipher(values.password).exec((err, hash) => {
if(err){
return cb(err);
}
values.password = hash;
cb();
})
}
}
这是我的帮手(密码)
const bcrypt = require('bcryptjs');
module.exports = {
friendlyName: 'Chiper helper',
description: 'Chiper helper for password user and others',
inputs: {
password: {
description: 'user password',
type: 'string',
required: true
}
},
exits: {
success: {
description: 'success by default JSON'
},
notFound: {
description: 'erro, not found',
responseType: 'notFound'
}
},
fn: async (inputs, exits) => {
const { password } = inputs;
bcrypt.hashSync(password, 10, (err, hash) => {
if(err) return exits.notFound();
return exits.success(hash);
});
}
}
因为你用的是bcrypt.hashSync
,密码哈希是同步完成的,没有回调也没有async
关键字,你需要改fn
到此 :
fn: (inputs, exits) => {
const { password } = inputs;
var hash = bcrypt.hashSync(password, 10);
return exits.success(hash);
}
如果您想以 异步 方式进行操作,您可以这样做:
fn: (inputs, exits) => {
const { password } = inputs;
bcrypt.hash(password, 10, (err, hash) => {
if(err) return exits.notFound();
return exits.success(hash);
});
}
您不需要使用 async
关键字,因为我们只使用回调。
好吧,我做了一个加密密码的助手,我试图在我的用户模型的生命周期中使用它,但是当我调用请求时,它进入了一种循环,它 returns没有回应。
这是我的 action2(创建用户)[路线:'post /user':'user/create']
module.exports = {
friendlyName: 'Create user',
description: 'action create user',
inputs: {
name: {
description: 'user name',
type: 'string',
required: true
},
username: {
description: 'username unique',
type: 'string',
required: true
},
email: {
description: 'user email',
type: 'string',
required: true
},
password: {
description: 'user password',
type: 'string',
required: true
},
nivel: {
description: 'user nivel access (see in polices)',
type: 'string',
required: false
},
status: {
description: 'user status',
type: 'string',
required: false
}
},
exits: {
success: {
description: 'success by default JSON'
},
notFound: {
description: 'not found',
responseType: 'notFound'
}
},
fn: async (inputs, exits) => {
const { name, username, email, password, nivel, status } = inputs;
var user = await User.create({ name, username, email, password, nivel, status }).fetch();
if(!user) return exits.notFound();
return exits.success(user);
}
}
这是我的模型(用户)
module.exports = {
attributes: {
name: {
type: 'string',
required: true
},
username: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'string',
unique: true,
required: true,
isEmail: true
},
password: {
type: 'string',
required: true,
minLength: 3
},
passwordResetExpires: {
type: 'string',
},
nivel: {
type: 'string',
isIn: ['developer', 'administrator', 'attendant'],
defaultsTo: 'attendant'
},
status: {
type: 'string',
isIn: ['activated', 'disabled'],
defaultsTo: 'activated'
}
},
customToJSON: () => {
return _.omit(this, ['password', 'passwordResetExpires']);
},
// Lifecycle Callbacks
beforeCreate: (values, cb) => {
//cipher helper
sails.helpers.cipher(values.password).exec((err, hash) => {
if(err){
return cb(err);
}
values.password = hash;
cb();
})
}
}
这是我的帮手(密码)
const bcrypt = require('bcryptjs');
module.exports = {
friendlyName: 'Chiper helper',
description: 'Chiper helper for password user and others',
inputs: {
password: {
description: 'user password',
type: 'string',
required: true
}
},
exits: {
success: {
description: 'success by default JSON'
},
notFound: {
description: 'erro, not found',
responseType: 'notFound'
}
},
fn: async (inputs, exits) => {
const { password } = inputs;
bcrypt.hashSync(password, 10, (err, hash) => {
if(err) return exits.notFound();
return exits.success(hash);
});
}
}
因为你用的是bcrypt.hashSync
,密码哈希是同步完成的,没有回调也没有async
关键字,你需要改fn
到此 :
fn: (inputs, exits) => {
const { password } = inputs;
var hash = bcrypt.hashSync(password, 10);
return exits.success(hash);
}
如果您想以 异步 方式进行操作,您可以这样做:
fn: (inputs, exits) => {
const { password } = inputs;
bcrypt.hash(password, 10, (err, hash) => {
if(err) return exits.notFound();
return exits.success(hash);
});
}
您不需要使用 async
关键字,因为我们只使用回调。