Mongoose ValidationError:验证失败:需要路径

编程入门 行业动态 更新时间:2024-10-09 18:16:27

Mongoose ValidationError:验证失败:需要<a href=https://www.elefans.com/category/jswz/34/1771438.html style=路径"/>

Mongoose ValidationError:验证失败:需要路径

当我在Web应用程序中以与我的mongoDB模式有关的ejs形式输入信息时,出现错误:

错误即时消息:

ValidationError: Subscriber validation failed: grade1: Path `grade1` is required., grade2: Path `grade2` is required., grade3: Path `grade3` is required., grade4: Path `grade4` is required.

我的订户模式:

const mongoose = require("mongoose");
const subscriberSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true
  },
  grade1: {
    type: String,
    required: true
  },
  grade2: {
    type: String,
    required: true
  },
  grade3: {
    type: String,
    required: true
  },
  grade4: {
    type: String,
    required: true
  }
});

subscriberSchema.methods.getInfo = function() {
  return `Name: ${this.name} grade1: ${this.grade1} grade2: ${this.grade2} grade3: ${this.grade3} grade4: ${this.grade4}`;
};

subscriberSchema.methods.findLocalSubscribers = function() {
  return this.model("Subscriber")
    .find({
      name: this.name
    })
    .exec();
};

ejs形式:

<div class="col-sm-6">
  <h1>Enter Student Name and Grades</h1>
  <p>Enter the student's name and grades for the first 4 CSC courses:</p>
  <form class="subscription-form" action="/subscribers/create" method="post">
    <input type="text" name="name" placeholder="Name" autofocus>
    <input type="text" grade1="grade1" placeholder="CSC141 Grade" required>
    <input type="text" grade2="grade2" placeholder="CSC142 Grade" required>
    <input type="text" grade3="grade3" placeholder="CSC240 Grade" required>
    <input type="text" grade4="grade4" placeholder="CSC241 Grade" required>
    <input type="submit" name="submit">
  </form>
</div>

订阅者/在订阅者控制器中创建:

  create: (req, res, next) => {
    let subscriberParams = {
      name: req.body.name,
      grade1: req.body.grade1,
      grade2: req.body.grade2,
      grade3: req.body.grade3,
      grade4: req.body.grade4
    };
    Subscriber.create(subscriberParams)
      .then(subscriber => {
        res.locals.redirect = "/subscribers";
        res.locals.subscriber = subscriber;
        next();
      })
      .catch(error => {
        console.log(`Error saving subscriber: ${error.message}`);
        next(error);
      });
  },
...

我是mongoDB / nodejs / ejs的新手,所以我想知道我在做什么错。

回答如下:它适用于name,因为您正在使用HTML格式name格式,请尝试:

<input type="text" name="grade1" placeholder="CSC141 Grade" required> <input type="text" name="grade2" placeholder="CSC142 Grade" required> <input type="text" name="grade3" placeholder="CSC240 Grade" required> <input type="text" name="grade4" placeholder="CSC241 Grade" required>

更多推荐

Mongoose ValidationError:验证失败:需要路径

本文发布于:2024-05-07 06:01:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754673.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   Mongoose   ValidationError

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!