我的路径验证失败 MERN 中的错误

问题描述

我的 controller.js 代码

const User = require("../models/user") const express = require("express")

exports.signup = (req,res) => {   const user = new User(req.body);   user.save((err,user) => {
    console.log(err) 
    if (err) {
      return res.status(400).json({
        err: "NOT able to save user in DB"
      });
    }
    res.json(user)({
      name: user.name,email: user.email,id: user._id
    });   }); };

exports.signout = (req,res) => {   res.send("User signout") };

我的 user.js 代码

const mongoose = require ("mongoose"); const Schema = mongoose.Schema; const crypto = require('crypto'); const { v4: uuidv4 } = require('uuid');

var userSchema = new mongoose.Schema({
    name:{
        type: String,required: true,maxlength: 32,trim: true
    },lastname: {
        type: String,trim: true,},email: {
        type: String,required : true,unique: true
    },userinfo: {
        type: String,//todo come back here
    encry_password: {
        type: String,salt: String,role: {
        type: Number,default: 0
    },purchases: {
        type: Array,default: []
    }


},{timestamps: true} );

userSchema.virtual("password")
    .set(function(password){
        this._password = passsword
        this.salt = uuidv4();
        this.encry_password = this.securepassword(password)
    })
    .get(function(){
        return this._password
    })


userSchema.methods = {
    authenticate: function(plainpassword){
        return this.securepassword(plainpassword) === this.encry_password
    },securepassword: function(plainpassword){
        if (!plainpassword) return "";
        try{
            return crypto.createHmac('sha256',this.salt)
            .update('plainpassword')
            .digest('hex');
        } catch (err) {
            return "";

        }
    } }

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

下面是我得到的错误

[nodemon] 开始 node app.js 应用程序运行在 8000 数据库连接 错误用户验证失败:encry_password:需要路径 encry_password。,电子邮件:需要路径 email。,名称:需要路径 name。 在 ValidationError.inspect (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\error\validation.js:47:26) 在内部/per_context/primordias.js:23:32 在 formatValue (internal/util/inspect.js:783:19) 在检查 (internal/util/inspect.js:337:10) 在 formatWithOptionsInternal (internal/util/inspect.js:2016:40) 在 formatWithOptions (internal/util/inspect.js:1898:10) 在 console.value (internal/console/constructor.js:323:14) 在 console.log (internal/console/constructor.js:358:61) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\controllers\auth.js:7:13 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\model.js:4870:16 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\helpers\promiSEOrCallback.js:16:11 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\model.js:4893:21 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\model.js:500:16 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\kareem\index.js:247:48 接下来 (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\kareem\index.js:168:27) 接下来 (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\kareem\index.js:170:9) { 错误:{ encry_password: ValidatorError: 路径 encry_password 是必需的。 在验证 (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1251:13) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1234:7 在 Array.forEach() 在 SchemaString.SchemaType.dovalidate (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1179:14) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\document.js:2516:18 在 processticksAndRejections (internal/process/task_queues.js:75:11) { 属性:[对象], 种类:'必需', 路径:'encry_password',值:未定义, 原因:未定义, [符号(猫鼬:验证器错误)]:真 },电子邮件:验证器错误:路径 email 是必需的。 在验证 (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1251:13) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1234:7 在 Array.forEach() 在 SchemaString.SchemaType.dovalidate (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1179:14) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\document.js:2516:18 在 processticksAndRejections (internal/process/task_queues.js:75:11) { 属性:[对象], 种类:'必需', 路径:'电子邮件', 值:未定义, 原因:未定义, [符号(猫鼬:验证器错误)]:真 },名称:ValidatorError:需要路径 name。 在验证 (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1251:13) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1234:7 在 Array.forEach() 在 SchemaString.SchemaType.dovalidate (C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\schematype.js:1179:14) 在 C:\Users\Harshal\Desktop\mernbootcamp\projbackend\node_modules\mongoose\lib\document.js:2516:18 在 processticksAndRejections (internal/process/task_queues.js:75:11) { 属性:[对象], 种类:'必需', 路径:'名称', 值:未定义, 原因:未定义, [符号(猫鼬:验证器错误)]:真 } },_message: '用户验证失败' }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)