注册不工作并抛出params错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了注册不工作并抛出params错误相关的知识,希望对你有一定的参考价值。

我目前能够使用以前创建的用户凭据登录,并正常使用该应用程序,但无法创建新用户。我在客户端使用React.js,在后端使用Express api。我收到了猫鼬验证错误。所有的身份验证都附带了课程让我们使用的模板,我没有触及任何这些文件。我甚至回过头来比较提交历史树以确保没有任何改变。

这是我的用户架构和注册路由。我尝试从模型中消除唯一性,但这并没有影响它。我知道有很多潜在的地方可能会出错,但如果有人对潜在问题有任何建议,我将永远感激不尽!我在注册表中记录了req.body.credentials并且发送的数据看起来很好。

错误代码:422无法处理的实体服务器端错误:'收到的参数未通过Mongoose验证'

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))
})
答案

解决了。我在用户中使用的一个子文档有一个值设置为唯一的键。我需要消除这一点,因为我的数据库使用空值索引用户并抛出重复的错误。然后我需要重置我的数据库(我只是将其重命名为测试它),以便它没有任何已保存的索引与该配置。我刚刚在Heroku中删除了我的集合(幸运的是,我没有大量的数据,这个解决方案对我的情况来说非常好)。我现在能够再次注册用户,没有任何重复的密钥错误。

以上是关于注册不工作并抛出params错误的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot集成测试无法在命令行未指定配置文件时获取默认配置文件并抛出错误

Laravel 5 App - AJAX Post请求不接受令牌并抛出500内部服务器错误

我的查询根本不起作用并抛出错误

从 github 获取此代码,尝试运行它并抛出此错误,据我所知,错误在 index.js 中,我在此处提供了 index.js 代码 [重复]

为啥spring websocket应用看不到html并抛出404错误?

我的微服务没有启动并抛出错误