Typeerror: isPassword is not a Function

Asked

Viewed 284 times

0

I’m having this error: Typeerror: isPassword is not a Function. I did a lot of web searches, found similarities, but I still can’t solve it. So another way I solved, but as I’m starting the studies I wanted to know the pq does not pass, where I may be wrong in creating a classMethod and using it:

I created this js:

import bcrypt from "bcrypt";

module.exports = (sequelize, DataType) => {
  const Users = sequelize.define("usuarios", {
    id: {
      type: DataType.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    nome: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    password: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      },
      field : 'senha'
    },
    email: {
      type: DataType.STRING,
      unique: true,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    datacadastro: {
      type: DataType.DATE,
      defaultValue: new Date()
    }
  }, 
  {
      hooks: {
          beforeCreate: user => {
             const salt = bcrypt.genSaltSync();
             user.password = bcrypt.hashSync(user.password, salt);
          }
        },
      classMethods: {
          **//estou tendo erro em utilizar este isPassword**
          isPassword: (encodedPassword, password) => {
            return bcrypt.compareSync(password, encodedPassword);
          }
        }
  });
  return Users;
};

Here in this token.js that will be used to validate the email and password:

import jwt from "jwt-simple";

module.exports = app => {
  const cfg = app.libs.config;
  const Users = app.db.models.usuarios;

  app.post("/token", (req, res) => {
    if (req.body.email && req.body.password) {
      const email = req.body.email;
      const password = req.body.password;
      Users.findOne({where: {email: email}})
        .then(user => {
          **//Na linha abaixo que acontece o erro citado**
          if (Users.isPassword(user.password, password)) {
            const payload = {id: user.id};
            res.json({
              token: jwt.encode(payload, cfg.jwtSecret)
            });
          } else {
            res.sendStatus(401);
          }
        })
        .catch(error => res.sendStatus(401));
    } else {
      res.sendStatus(401);
    }
  });
};
  • First of all, what is the first file? A file that defines the template for Users? Second, where do you import / require the first file?

  • I’m sorry if I didn’t explain it very well, but this is it.

  • Already tried to define class method so: const Users = ...; Users.isPassword = ...; return Users

  • Sorry ignorance but as I am starting in Node js I did not try no: if (Users.methods.isPassword(user.password, password)) { .. would be so said for me to try gave error: Typeerror: Cannot read Property 'isPassword' of Undefined

  • Comments everything inside the callback of then. Set a function whatever of a log console, for example, as I said before. After defining Users with sequelize.define(), just say Users.qualquerFuncao = () => console.log('qualquerFuncao!!'); There inside the callback I mentioned at the beginning, calls the class function: Users.qualquerFuncao(). If this works, then your problem will be solved.

  • Thanks friend I found a way to solve, with your help and tips I found a link about sequelize that show how to migrate and what changed from one version to another, as I said I am at the beginning I took a tutorial to learn because I did not pay attention to the versions of frameworks.

  • It is always good to mention the version of the tool and always read the documentation referring to such version. This happens a lot when the main version changes from one to another.

Show 2 more comments

1 answer

1

To whom it may interest I found pq of this error in this link:

http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html

In short it’s version difference sequelize the User.js (model) got like this:

import bcrypt from "bcrypt";

module.exports = (sequelize, DataType) => {
  const Users = sequelize.define("usuarios", {
    id: {
      type: DataType.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    nome: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    password: {
      type: DataType.STRING,
      allowNull: false,
      validate: {
        notEmpty: true
      },
      field : 'senha'
    },
    email: {
      type: DataType.STRING,
      unique: true,
      allowNull: false,
      validate: {
        notEmpty: true
      }
    },
    datacadastro: {
      type: DataType.DATE,
      defaultValue: new Date()
    }
  }, {
    hooks: {
      beforeCreate: user => {
        const salt = bcrypt.genSaltSync();
        user.password = bcrypt.hashSync(user.password, salt);
      }
   }
 });

  Users.isPassword = (encodedPassword, password) => {
    return bcrypt.compareSync(password, encodedPassword);
  }

  return Users;
};
  • So Voce ta using version 4, but had used settings for an old version? If you can, please add what version Voce used in your question. Thank you!

  • Because I was following a tutorial and I did not heed the version, vacillated, as I said starting studies and the version of the sequelize: 4.4.2

Browser other questions tagged

You are not signed in. Login or sign up in order to post.