[英]Sign Up not working and throwing params errors
I am currently able to sign in just fine with previously created user credentials and use the app as normal, but am unable to create a new user.我目前可以使用之前创建的用户凭据正常登录并正常使用该应用程序,但无法创建新用户。 I am using React.js on the client side and an Express api on the backend.
我在客户端使用 React.js,在后端使用 Express api。 I am getting a mongoose validation error.
我收到猫鼬验证错误。 All of the authentication came with the template the course has us use and I haven't touched any of those files.
所有身份验证都随课程使用的模板一起提供,我没有接触过任何这些文件。 I even went back and compared commit history trees to ensure that nothing was changed.
我什至回去比较提交历史树以确保没有任何更改。
Here is my user schema and sign-up route.这是我的用户架构和注册路线。 I tried eliminating uniqueness from the model and that didn't impact it.
我尝试从模型中消除唯一性,但这并没有影响它。 I know there is a lot of potential places something could be going wrong, but if anyone has any suggestions on potential issues I would be forever grateful!
我知道有很多潜在的地方可能会出错,但是如果有人对潜在问题有任何建议,我将永远感激不尽! I console logged the req.body.credentials within sign up and the data being sent over looks good.
我在注册时控制台记录了 req.body.credentials,发送的数据看起来不错。
Error code: 422 Unprocessable Entity Server side error: 'The received params failed a Mongoose validation'错误代码:422 无法处理的实体服务器端错误:“收到的参数未能通过猫鼬验证”
const mongoose = require('mongoose')
const { petSchema } = require('./pet.js')
const { pictureSchema } = require('./picture.js')
const userSchema = new mongoose.Schema({
email: {
type: String,
required: true,
unique: true
},
hashedPassword: {
type: String,
required: true
},
token: String,
pets: [petSchema],
pictures: [pictureSchema]
}, {
timestamps: true,
toObject: {
// remove `hashedPassword` field when we call `.toObject`
transform: (_doc, user) => {
delete user.hashedPassword
return user
}
}
})
module.exports = mongoose.model('User', userSchema)
// SIGN UP
// POST /sign-up
router.post('/sign-up', (req, res) => {
// start a promise chain, so that any errors will pass to `handle`
console.log(req.body.credentials)
Promise.resolve(req.body.credentials)
// reject any requests where `credentials.password` is not present, or where
// the password is an empty string
.then(credentials => {
if (!credentials ||
!credentials.password ||
credentials.password !== credentials.password_confirmation) {
throw new BadParamsError()
}
})
// generate a hash from the provided password, returning a promise
.then(() => bcrypt.hash(req.body.credentials.password, bcryptSaltRounds))
.then(hash => {
// return necessary params to create a user
return {
email: req.body.credentials.email,
hashedPassword: hash
}
})
// create user with provided email and hashed password
.then(user => User.create(user))
// send the new user object back with status 201, but `hashedPassword`
// won't be sent because of the `transform` in the User model
.then(user => res.status(201).json({ user: user.toObject() }))
// pass any errors along to the error handler
.catch(err => handle(err, res))
})
Solved.解决了。 One of the subdocuments I had within user had a key with a value set to unique.
我在 user 中拥有的子文档之一有一个键,其值设置为唯一。 I needed to eliminate that because my database was indexing users with a null value and throwing a duplicate error.
我需要消除它,因为我的数据库正在使用空值索引用户并抛出重复错误。 I then needed to reset my database (I just renamed it to test it out) so that it didn't have any saved indexes with that configuration.
然后我需要重置我的数据库(我只是重命名它以测试它),以便它没有任何保存的具有该配置的索引。 I just deleted my collections within Heroku as well (luckily I didn't have significant amounts of data in there and this solution was perfectly fine for my situation).
我也刚刚在 Heroku 中删除了我的集合(幸运的是我那里没有大量数据,这个解决方案非常适合我的情况)。 I am now able to sign up users again without any duplicate key errors.
我现在可以再次注册用户,而不会出现任何重复的密钥错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.