我正在使用 reactjs 和 express js 和 sequilize 做一个登录应用程序,当我使用 Postman API 测试它时,一切正常,但是当我使用 reactjs 尝试它时,它在后端给了我这个问题
model/user.model.js
后端中的代码
module.exports = (sequelize, Sequelize) => {
const User = sequelize.define('users', {
name: {
type: Sequelize.STRING
},
number_phone: {
type: Sequelize.INTEGER
},
password: {
type: Sequelize.STRING
}
});
return User;
}
路由器/验证注册.js
后端中的代码
const db = require('../config/db.config.js');
const config = require('../config/config.js');
const ROLEs = config.ROLEs;
const User = db.user;
const Role = db.role;
checkDuplicatePhone = (req, res, next) => {
// -> Check Username is already in use
User.findOne({
where: {
number_phone: req.body.number_phone
}
}).then(user => {
if(user){
res.status(400).send("Fail -> Phone is already taken!");
return;
}
next();
});
}
checkRolesExisted = (req, res, next) => {
for(let i=0; i<req.body.roles.length; i++){
if(!ROLEs.includes(req.body.roles[i].toUpperCase())){
res.status(400).send("Fail -> Does NOT exist Role = " + req.body.roles[i]);
return;
}
}
next();
}
const signUpVerify = {};
signUpVerify.checkDuplicatePhone = checkDuplicatePhone;
signUpVerify.checkRolesExisted = checkRolesExisted;
module.exports = signUpVerify;
控制器/控制器.js
后端中的代码
const db = require('../config/db.config.js');
const config = require('../config/config.js');
const User = db.user;
const Role = db.role;
const Op = db.Sequelize.Op;
var jwt = require('jsonwebtoken');
var bcrypt = require('bcryptjs');
exports.signup = (req, res) => {
// Save User to Database
console.log("Processing func -> SignUp");
User.create({
name: req.body.name,
number_phone: req.body.number_phone,
password: bcrypt.hashSync(req.body.password, 8)
}).then(user => {
Role.findAll({
where: {
name: {
[Op.or]: req.body.roles
}
}
}).then(roles => {
user.setRoles(roles).then(() => {
res.send("User registered successfully!");
});
}).catch(err => {
res.status(500).send("Error -> " + err);
});
}).catch(err => {
res.status(500).send("Fail! Error -> " + err);
})
}
exports.signin = (req, res) => {
console.log("Sign-In");
User.findOne({
where: {
number_phone: req.body.number_phone
}
}).then(user => {
if (!user) {
return res.status(404).send('User Not Found.');
}
var passwordIsValid = bcrypt.compareSync(req.body.password, user.password);
if (!passwordIsValid) {
return res.status(401).send({ auth: false, accessToken: null, reason: "Invalid Password!" });
}
var token = jwt.sign({ id: user.id }, config.secret, {
expiresIn: 86400 // expires in 24 hours
});
res.status(200).send({ auth: true, accessToken: token });
}).catch(err => {
res.status(500).send('Error -> ' + err);
});
}
exports.userContent = (req, res) => {
User.findOne({
where: { id: req.userId },
attributes: ['name', 'number_phone'],
include: [{
model: Role,
attributes: ['id', 'name'],
through: {
attributes: ['userId', 'roleId'],
}
}]
}).then(user => {
res.status(200).json({
"description": "User Content Page",
"user": user
});
}).catch(err => {
res.status(500).json({
"description": "Can not access User Page",
"error": err
});
})
}
exports.adminBoard = (req, res) => {
User.findOne({
where: { id: req.userId },
attributes: ['name', 'number_phone'],
include: [{
model: Role,
attributes: ['id', 'name'],
through: {
attributes: ['userId', 'roleId'],
}
}]
}).then(user => {
res.status(200).json({
"description": "Admin Board",
"user": user
});
}).catch(err => {
res.status(500).json({
"description": "Can not access Admin Board",
"error": err
});
})
}
exports.managementBoard = (req, res) => {
User.findOne({
where: { id: req.userId },
attributes: ['name', 'number_phone'],
include: [{
model: Role,
attributes: ['id', 'name'],
through: {
attributes: ['userId', 'roleId'],
}
}]
}).then(user => {
res.status(200).json({
"description": "Management Board",
"user": user
});
}).catch(err => {
res.status(500).json({
"description": "Can not access Management Board",
"error": err
});
})
}
我只是在邮递员中尝试。
邮 编 : http://localhost:4242/auth/signup
未处理的拒绝错误:WHERE 参数"number_phone"具有无效的"未定义"值
谢谢!
从 Sequelize 5.0 开始,where
子句中不能有undefined
键:
续集将抛出所有未定义的键,其中选项,在过去的版本中,未定义被转换为 null。
确保在请求正文中传递number_phone
或将|| null
添加到 where 子句以默认为 null。