由于某种原因,每当我向路由发出请求以创建新用户时,尽管创建了该用户,但我仍会收到“此电子邮件帐户已在使用中”,而当我尝试登录时,我会得到“登录信息为错误”。我在登录时用控制台登录了req.body,并且记录了对象,看起来还可以。
我的AuthenticationController.js
const {User} = require('../models')
const jwt = require('jsonwebtoken')
const config = require('../config/config')
function jwtSignUser (user) {
const ONE_WEEK = 60 * 60 * 24 * 7
return jwt.sign(user, config.authentication.jwtSecret, {
expiresIn: ONE_WEEK
})
}
module.exports = {
async register (req, res) {
try {
const user = await User.create(req.body)
const userJson = user.toJSON()
res.send({
user: userJson,
token: jwtSignUser(userJson)
})
} catch (err) {
res.status(400).send('This email account is already in use.')
}
},
async login (req, res) {
console.log(req.body)
try {
const {email, password} = req.body
const user = await User.findOne({
where: {
email: email
}
})
if (!user) {
return res.status(403).send({
error: 'The login information was incorrect'
})
}
const isPasswordValid = await user.comparePassword(password)
if (!isPasswordValid) {
return res.status(403).send({
error: 'The login information was incorrect'
})
}
const userJson = user.toJSON()
res.send({
user: userJson,
token: jwtSignUser(userJson)
})
} catch (err) {
res.status(500).send({
error: 'An error has occured trying to log in'
})
}
}
}
console.log(用户)为我提供以下内容:
User {
dataValues:
{ id: 41,
username: 'testing',
email: 'testing@gmail.com',
password:
'$2a$08$J6/psKuCltiCpCll2rjz4OaCylud0zq/wKnjKK8Udmm.bFU3c2Tt6',
firstName: 'test',
lastName: 'ing',
createdAt: 2019-02-26T15:47:09.133Z,
updatedAt: 2019-02-26T15:47:09.133Z },
_previousDataValues:
{ id: 41,
username: 'testing',
email: 'testing@gmail.com',
password:
'$2a$08$J6/psKuCltiCpCll2rjz4OaCylud0zq/wKnjKK8Udmm.bFU3c2Tt6',
firstName: 'test',
lastName: 'ing',
createdAt: 2019-02-26T15:47:09.133Z,
updatedAt: 2019-02-26T15:47:09.133Z },
_changed: {},
_modelOptions:
{ timestamps: true,
validate: {},
freezeTableName: false,
underscored: false,
underscoredAll: false,
paranoid: false,
rejectOnEmpty: false,
whereCollection: { email: 'testing@gmail.com' },
schema: null,
schemaDelimiter: '',
defaultScope: {},
scopes: [],
indexes: [],
name: { plural: 'Users', singular: 'User' },
omitNull: false,
hooks:
{ beforeCreate: [Array],
beforeUpdate: [Array],
beforeSave: [Array] },
sequelize:
Sequelize {
options: [Object],
config: [Object],
dialect: [SqliteDialect],
queryInterface: [QueryInterface],
models: [Object],
modelManager: [ModelManager],
connectionManager: [ConnectionManager],
importCache: [Object],
test: [Object] },
uniqueKeys: { Users_email_unique: [Object] } },
_options:
{ isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
raw: true,
attributes:
[ 'id',
'username',
'email',
'password',
'firstName',
'lastName',
'createdAt',
'updatedAt' ] },
__eagerlyLoadedAssociations: [],
isNewRecord: false }
console.log(req.body)给了我这个
{ email: 'testing@gmail.com',
password: 'testing99',
username: 'testing',
firstName: 'test',
lastName: 'ing' }
他们有相同的信息,但无论如何还是给我一个错误。
答案 0 :(得分:0)
根据文档https://mongoosejs.com/docs/api.html#model_Model.findOne mongoose
返回findOne
对象,我假设您正在使用Query
,因此验证可能会失败。
使用调试器甚至简单的console.log
检查登录功能中user
变量的内容。
因此,如果用户不存在,您将得到Query
对象,该对象不为空,因为mongoose
用某些功能填充了该对象。
您可以尝试使用lean()
,查看本文http://www.tothenew.com/blog/high-performance-find-query-using-lean-in-mongoose-2/
答案 1 :(得分:0)
我们在这里有2种解决方案:
使用raw:true
const user = await User.findOne({ 其中:{ 电子邮件:电子邮件 }, 原始:真实 })
使用纯文字:true
user = user.get({ 平原:真实 });