简体   繁体   English

为什么在添加.method时将其var返回为undefined?

[英]Why is my var being returned as undefined when adding .methods to it?

I'm attempting to add bycrypt to an authentication table but I'm getting an error that authTable is undefined when attempting to add the generateHash and validPassword methods. 我正在尝试将bycrypt添加到身份验证表中,但是在尝试添加generateHash和validPassword方法时收到错误消息authTable未定义。 Is there something i'm not understanding about the authTables.methods portion? 关于authTables.methods部分,我是否不了解?

The code works when I comment out the generateHash and validPassword portion which leads me to believe that .methods is the hang up. 当我注释掉generateHash和validPassword部分时,代码起作用了,这使我相信.methods是挂断的。

var bcrypt = require("bcryptjs");

module.exports = function (sequelize, DataTypes) {
  var authTable = sequelize.define("authTable", {

    username: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      len: [1, 30]
    }
  },
  password: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      len: [6, 20]
    }
  },
  email: {
    type: DataTypes.STRING,
    allowNull: false,
    validate: {
      isEmail: true,
      len: [1]
    }
  }
});

authTable.methods.generateHash = function(password) {
  return bcrypt.hashSync(password, bcrypt.genSaltSync(10), null);
};

authTable.methods.validPassword = function(password) {
  return bcrypt.compareSync(password, this.local.password);
};

return authTable;
}

I would expect this to go to the table within the database with an encrypted password. 我希望它会使用加密的密码进入数据库中的表。

The errors i'm receiving are: 我收到的错误是:

TypeError: Cannot set property 'generateHash' of undefined. TypeError:无法将属性'generateHash'设置为undefined。

TypeError: Cannot set property 'validPassword' of undefined TypeError:无法设置未定义的属性“ validPassword”

I'm getting an error that authTable is undefined 我收到一个错误消息,即authTable未定义

No, you don't. 不,你没有。 You get an error that authTable.methods is undefined. 您会收到一个错误, authTable.methods未定义。

define returns an instance of Model , and as you can see in the documentation of Model , it does not have a property named methods , ergo, authTable.methods will evaluate to undefined . define返回Model一个实例,正如您在Model的文档中所见,它没有名为methods的属性,因此ergo, authTable.methods将为undefined

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM