模型生命周期中的助手Sails 1

时间:2018-04-19 02:31:58

标签: node.js sails.js

好吧,我做了一个加密密码的助手,我试图在我的用户模型的生命周期中使用它,但是当我调用请求时,它会进入一种循环,返回没有回应。

这是我的动作2(创建用户)[路线:'帖子/用户' :' 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);
    });

  }

}

1 个答案:

答案 0 :(得分:0)

这是因为您使用的是bcrypt.hashSync,密码哈希是同步,没有回调且没有async关键字,您需要更改{ {1}}对此:

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); }); } 关键字,因为我们只使用回调。