我在 MongoDB 中收到错误“错误:用户验证失败”

苛刻的米什拉

尝试使用该POST方法postman注册用户后出现错误。Schemaregister路线创建了一个简单的。

错误:

Error in register: Error: User validation failed: password: Cast to string failed for value "Promise { <pending> }" (type Promise) at path "password"
    at ValidationError.inspect (/Users/*****/authSystem/node_modules/mongoose/lib/error/validation.js:48:26)
    at formatValue (node:internal/util/inspect:782:19)
    at inspect (node:internal/util/inspect:347:10)
    at formatWithOptionsInternal (node:internal/util/inspect:2167:40)
    at formatWithOptions (node:internal/util/inspect:2029:10)
    at console.value (node:internal/console/constructor:324:14)
    at console.log (node:internal/console/constructor:360:61)
    at /Users/harshmishra/Desktop/BackendApp/authSystem/app.js:46:17
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {

  errors: {
    password: CastError: Cast to string failed for value "Promise { <pending> }" (type Promise) at path "password"
        at SchemaString.cast (/Users/*****/authSystem/node_modules/mongoose/lib/schema/string.js:600:11)
        at SchemaString.SchemaType.applySetters (/Users/*****/authSystem/node_modules/mongoose/lib/schematype.js:1189:12)
        at model.$set (/Users/***/authSystem/node_modules/mongoose/lib/document.js:1409:20)
        at model.$set (/Users/*****/authSystem/node_modules/mongoose/lib/document.js:1137:16)
        at model.Document (/Users/****/authSystem/node_modules/mongoose/lib/document.js:162:12)
        at model.Model (/Users/****/authSystem/node_modules/mongoose/lib/model.js:115:12)
        at new model (/Users/*****/authSystem/node_modules/mongoose/lib/model.js:4825:15)
        at /Users*****/authSystem/node_modules/mongoose/lib/model.js:3132:22
        at /Users/*****/authSystem/node_modules/mongoose/lib/model.js:3168:7
        at Array.forEach (<anonymous>) {
      stringValue: '"Promise { <pending> }"',
      messageFormat: undefined,
      kind: 'string',
      value: [Promise],
      path: 'password',
      reason: null,
      valueType: 'Promise'
    }
  },
  _message: 'User validation failed'
}

架构:

const userSchema = new mongoose.Schema(
  {
    firstName: {
      type: String,
      default: null,
    },
    lastName: {
      type: String,
      default: null,
    },
    email: {
      type: String,
      unique: true,
    },
    password: {
      type: String,
    },
    token: {
      type: String,
    },
  },
);

module.exports = mongoose.model('User', userSchema)

/登记:

app.post("/register", async(req, res) => {
    try {
        const { firstName, lastName, email, password } = req.body;
        if (!(firstName && lastName && email && password)) {
         return res.status(400).send("All fields are required");
        }
        const existingUser =User.findOne({ email });

        if (existingUser) {
          return res.status(401).send("User already exist");
        }
        const myEncrpytedPass = await bcryptjs.hash(password, 10);
        const user = await User.create({
          firstName,
          lastName,
          email: email.toLowerCase(),
          password: myEncrpytedPass,
        });
        const token = await jwt.sign(
          {
            user_id: user._id,
            email,
          },
          process.env.SECRET_KEY,
          {
            expiresIn: "4h",
          }
        );
        user.token = token;
      return res.status(201).json(user);
    } catch (error) {
        console.log("Error in register:",error)
   }
})

我无法弄清楚我在这里做错了什么?正如我遵循猫鼬的文档,但我仍然收到错误。

我应该在这里做些什么改变?

氖钠

您需要更改两件事:

第一的

尝试awaitbcryptjs.hash(). 看起来该函数返回了一个 Promise。

const myEncrpytedPass = await bcryptjs.hash(password, 10);

第二

return每个res.send.

例如,如果用户存在,您将发送 this res.send(401).send("User already exist"),但由于您没有添加return,功能将继续,并会到达下一个res.status(201).json(user)响应。这将引发错误,Cannot set headers after they are sent to the client因为您已经将响应发送给用户。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我收到Auth错误:由于[java.io.FileNotFoundException:身份验证失败:在Android Studio中实施pusher-js时

我收到此错误“处理清单失败”

我收到此错误“用户”对象在联系表单中没有属性“获取”

使用pymodm连接MongoDB服务器的Python脚本中的身份验证失败错误

我收到“模板参数推导/替换失败:” C ++错误

为什么我收到“验证约束无错误”?

我收到此错误

我在Qt中收到错误:SIGILL ERROR

在Java中,我收到-Xlint:unchecked错误

我在VHDL中收到以下错误:无法链接设计过程“模拟行为模型失败”

我从ArrayList中收到错误

我收到休眠错误

我在vba中收到呼叫功能错误?

从我的Java程序中收到许多错误

验证错误Mongodb

我在SQL中收到语法错误

如果我尝试在 Template10-Validation 中自定义错误验证,我会收到 DependencyProperty 错误

MongoDB - 验证设置错误

Mongodb 用户架构错误

尝试部署业务网络时,我在 grpc 中收到错误“解析 HTTP/2 失败”

我收到错误 TemplateDoesNotExist

我使用 express 和 mongoDB 使用 findByIdAndUpdate 收到 500 个“内部服务器错误”

我无法在我的 Ubuntu 系统中安装 MongoDB,并且在安装时遇到此错误

我在sqlite中收到双引号错误

即使用户已登录,我也会收到错误消息

python中的Mongodb ssl证书错误,我该如何修复错误?

我在 python 程序中收到值错误

为什么我会收到身份验证错误?

我试图接收用户的输入,但我不断收到此错误。为什么我会收到此错误?