【问题标题】:Helpers in model life cycle Sails 1模型生命周期中的助手 Sails 1
【发布时间】:2018-04-19 02:31:58
【问题描述】:

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

这是我的 action2(创建用户)[Route: '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);
    });

  }

}

【问题讨论】:

    标签: node.js sails.js


    【解决方案1】:

    因为你用的是bcrypt.hashSync,密码hash是同步做的,没有回调也没有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 关键字,因为我们只使用回调。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-20
      • 2019-01-04
      相关资源
      最近更新 更多