为什么 Id 只保存在数据库中而没有其他数据?
Why is Id only getting saved in database and no other data?
我正在使用 Passport、sequelize 和 MySql 向我的 React 应用程序添加身份验证,但数据没有保存到 mySql 数据库中,只有 Id 和 null 字段。
我正在尝试实施本地策略。
在 postman 中尝试 post 路由时,我收到 200 响应,其中包含电子邮件和散列密码。但是,ID 字段显示为空。检查 mysql 数据库时,我可以看到添加了 ID 的新行,但用户名和密码为空。我在这里遗漏了什么吗?
护照策略文件
const Strategy = require('passport-local').Strategy;
const User = require('../models').User;
const bcrypt = require('bcryptjs');
const salt = bcrypt.genSaltSync(10);
const SignupStrategy = new Strategy({ passReqToCallback: true,
usernameField: "email", passwordField: "password" }, function (req, email,
password, done) {
User.findOne({
where: {
email: email
}
})
.then((user, error) => {
if (error)
return done(null, user);
})
const encryptedPassword = bcrypt.hashSync(password, salt);
let newUser = new User({
email,
password: encryptedPassword
})
User.create(newUser)
.then((user) => {
newUser
;
done(null, newUser)
})
.catch((error) => {
done(error, null)
})
});
module.exports = SignupStrategy;
用户模型
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING,
// allowNull: false,
unique: true,
validate: {
len: [1]
}
},
email: {
type: DataTypes.STRING,
// allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataTypes.STRING,
// allowNull: false
}
});
return User;
};
路线
const express = require('express');
const router = express.Router();
const passport = require('../passport');
router.post('/signup', (req, res, next) => {
passport.authenticate('local-signup', function(error, user, info) {
if (error) {
res.status(500).json({
message: 'Ooops, something happened',
error: error.message || 'Internal server error'
});
}
res.json({user})
})(req, res, next)
});
router.post('/signin', function(req, res, next) {
res.send('respond with a resource')
});
module.exports = router;
护照索引文件
const passport = require('passport');
const SignupStrategy = require('./SignupStrategy');
const SigninStrategy = require('./SigninStrategy');
passport.use('local-signin', SigninStrategy);
passport.use('local-signup', SignupStrategy);
module.exports = passport;
如果我在用户模型中将 allowNull 设置为 true,我将收到来自 sequelize 的验证错误(非空违规)。不知道现在该做什么
而不是这个:
let newUser = new User({
email,
password: encryptedPassword
})
使用这个:
let newUser = {
email,
password: encryptedPassword
};
There is no need to create any special object to create entry, it will
need just need simple json object.
我正在使用 Passport、sequelize 和 MySql 向我的 React 应用程序添加身份验证,但数据没有保存到 mySql 数据库中,只有 Id 和 null 字段。 我正在尝试实施本地策略。
在 postman 中尝试 post 路由时,我收到 200 响应,其中包含电子邮件和散列密码。但是,ID 字段显示为空。检查 mysql 数据库时,我可以看到添加了 ID 的新行,但用户名和密码为空。我在这里遗漏了什么吗?
护照策略文件
const Strategy = require('passport-local').Strategy;
const User = require('../models').User;
const bcrypt = require('bcryptjs');
const salt = bcrypt.genSaltSync(10);
const SignupStrategy = new Strategy({ passReqToCallback: true,
usernameField: "email", passwordField: "password" }, function (req, email,
password, done) {
User.findOne({
where: {
email: email
}
})
.then((user, error) => {
if (error)
return done(null, user);
})
const encryptedPassword = bcrypt.hashSync(password, salt);
let newUser = new User({
email,
password: encryptedPassword
})
User.create(newUser)
.then((user) => {
newUser
;
done(null, newUser)
})
.catch((error) => {
done(error, null)
})
});
module.exports = SignupStrategy;
用户模型
module.exports = function (sequelize, DataTypes) {
var User = sequelize.define("User", {
username: {
type: DataTypes.STRING,
// allowNull: false,
unique: true,
validate: {
len: [1]
}
},
email: {
type: DataTypes.STRING,
// allowNull: false,
unique: true,
validate: {
isEmail: true
}
},
password: {
type: DataTypes.STRING,
// allowNull: false
}
});
return User;
};
路线
const express = require('express');
const router = express.Router();
const passport = require('../passport');
router.post('/signup', (req, res, next) => {
passport.authenticate('local-signup', function(error, user, info) {
if (error) {
res.status(500).json({
message: 'Ooops, something happened',
error: error.message || 'Internal server error'
});
}
res.json({user})
})(req, res, next)
});
router.post('/signin', function(req, res, next) {
res.send('respond with a resource')
});
module.exports = router;
护照索引文件
const passport = require('passport');
const SignupStrategy = require('./SignupStrategy');
const SigninStrategy = require('./SigninStrategy');
passport.use('local-signin', SigninStrategy);
passport.use('local-signup', SignupStrategy);
module.exports = passport;
如果我在用户模型中将 allowNull 设置为 true,我将收到来自 sequelize 的验证错误(非空违规)。不知道现在该做什么
而不是这个:
let newUser = new User({
email,
password: encryptedPassword
})
使用这个:
let newUser = {
email,
password: encryptedPassword
};
There is no need to create any special object to create entry, it will need just need simple json object.