0
votes

When I submit my form I get the following error: Error [ValidationError]: System validation failed: lastName: Path lastName is required., firstName: Path firstName is required.

I'm not sure what's causing this, when I console.log(formData) I get the data I submitted into the form.

App.js

const express = require('express')
const app = express();
const mongoose = require('mongoose');
const dotenv = require ('dotenv/config');

const System = require('./models/System');

app.use(express.urlencoded({ extended: true }));
app.use(express.json());

app.get('/', (req,res) => {
  res.render('index.ejs');
});


app.post('/post-feedback', (req, res) => {
  const formData = {
    firstame: req.body.firstName,
    lastname: req.body.lastName,
    assetTag: req.body.assetTag
  }

  const system = new System(formData);

  system.save()
    .then(result => {
      console.log(result);
    })
    .catch(err => {
      console.log(err);
    });
});

Model:

const mongoose = require('mongoose');

var SystemSchema = new mongoose.Schema({
    firstName: {
        type: String,
        required: true
    },
    lastName: {
        type: String,
        required: true
    },
    assetTag: {
        type: Number,
        required: true
    }
});

module.exports = mongoose.model('System', SystemSchema);

Form:

      <form action="/post-feedback" method="POST">
                  <div class="form-group">
                    <label for="firstName">First Name: </label>
                       <input type="text" class="form-control" id="firstName" name="firstName">  
                  </div>
                  <div class="form-group">
                    <label for="lastName">Last Name: </label>
                       <input type="text" class="form-control" id="lastName" name="lastName">  
                  </div>
                  <div class="form-group">
                    <label for="assetNum">Asset Tag: </label>
                       <input type="text" class="form-control" id="assetTag" name="assetTag">  
                  </div>
                    <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                </form>
2
There are two things which you need to update first is the formdata object keys as they should match the schema and second assetTag should also be string. - KrishnaSingh

2 Answers

2
votes

The only one reason why you got some error it's because you're typo on your formData. It must be firstName and lastName, make sure it same with your Schema field and then Make Sure your assetTag is a number, because your Schema type is number.

You can try with this code below:

app.post('/post-feedback', (req, res) => {
  const formData = {
    // you're typo here: firstame
    firstName: req.body.firstName,
    // you're typo here: lastname
    lastName: req.body.lastName,
    // must be number
    assetTag: parseInt(req.body.assetTag);
  }

  const system = new System(formData);

  system.save()
    .then(result => {
      console.log(result);
    })
    .catch(err => {
      console.log(err);
    });
});

I hope it can help you.

0
votes
app.post('/post-feedback', (req, res) => {

   const system = new System(req.body);

  system.save()
    .then(result => {
      console.log(result);
    })
    .catch(err => {
      console.log(err);
    });
});

i think above code should be work.