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?
– nbkhope
I’m sorry if I didn’t explain it very well, but this is it.
– CÉSAR SANTOS
Already tried to define class method so:
const Users = ...; Users.isPassword = ...; return Users
– nbkhope
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
– CÉSAR SANTOS
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.– nbkhope
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.
– CÉSAR SANTOS
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.
– nbkhope