Home > Software design >  validation failed at Path is required, react, node and mongoose
validation failed at Path is required, react, node and mongoose

Time:07-04

I don't why this failed validation from the mongoose backend when I try to send the post request from the frontend, but if I test my backend API request with postman, it's working fine.

Feedback validation failed: receiver: Path receiver is required., classCodes: Path classCodes is required., level: Path level is required., content: Path content is required.

controller for my backend API

exports.createFeedback = async (req, res) => {
  try {
    console.log('data from frontend', req.body);
    const feedback = await new FeedbackSchema({
      receiver: req.body.receiver,
      teacher: req.userId,
      classCodes: req.body.classCodes,
      level: req.body.level,
      content: req.body.content,
      send: req.body.send,
    }).save();

    return res.json({ feedback, message: 'Feedback Created' });
    } catch (error) {
    console.log(error.message);
    res.json('Failed to create feedback!');
  }
};

Feedback Schema

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const FeedbackSchema = new Schema({
  receiver: {
    type: Schema.Types.ObjectId,
    ref: 'User',
    required: true,
  },
  teacher: {
    type: Schema.Types.ObjectId,
    ref: 'User',
  },
  level: {
    type: Schema.Types.ObjectId,
    ref: 'Level',
    required: true,
  },
  classCodes: {
    type: Schema.Types.ObjectId,
    ref: 'ClassCode',
    required: true,
  },
  content: {
    type: String,
    required: true,
    maxlength: 300,
  },
  note: {
    type: String,
    maxlength: 300,
  },
createdAt: {
    type: Date,
    default: Date.now(),
  },
});

module.exports = {
  FeedbackSchema: mongoose.model('Feedback', FeedbackSchema),
};

if I send a request from the postman is works fine without validation failed, but if I tried to send a request from the frontend, I receive a validation failed at Path above.

Note: don't border about the ratings schema

For frontend request. As you can see I log all the data before send it to the backend, content, level, receiver and classCodes and I also try to console.log(req.body) in the backend as well and I receive those data as well but still it show validation failed at Path is required.

Backend log

backend log

Frontend log

frontend log

Postman request worked fine

post api request is working fine

CodePudding user response:

You are sending the data nested in a values property, try to change your code like this:

exports.createFeedback = async (req, res) => {
    try {
      const { receiver, teacher, classCodes, level, content, send } = req.body.values;
      const feedback = await new FeedbackSchema({
        receiver,
        teacher,
        classCodes,
        level,
        content,
        send,
      }).save();
  
      return res.json({ feedback, message: 'Feedback Created' });
    } catch (error) {
      console.log(error.message);
      res.json('Failed to create feedback!');
    }
  };
  • Related