My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Need to send a message from Node.js to the Front-end EJS if any field left, and reload the page saying "Please fill all the details".

Arpit jain's photo
Arpit jain
·May 28, 2020

I'm creating a candidate registration page where the candidate should enter his details to get registered but I also want to show an error message if he misses to enter any field in the frontend and send back to the registration page saying "You need to fill all details". Any help will be appreciated!

This is controller

routes.post('/CreateCandidate' (req, res) => {
    var { ConsultantName, Location, job role, job type, Salary, Salarytype, experience, Availability, Skills, VisaStatus, BillingRate, resume } = req.body;

    console.log('okay');
    console.log(req.file);
    var Skills = req.body.Skills.split(',');
    Skills = Skills.map(function(x){ return x.toUpperCase() });
    var Candidateresume = req.file.filename; 
    var author = req.user.email;
   Candidate({ 
      author,
      ConsultantName,
      Location,
      JobRole,
      jobtype,
      Salary,
      Salarytype,
      experience,
      Availability,
      Skills,
      VisaStatus,
      BillingRate,
      resume
    }).save((err, data) => {
      res.redirect('/MyCandidates');
  });
  });

frontend is

<form action="/CreateCandidate" method="POST" enctype="multipart/form-data">

                <div class="form-group row">
                    <h4><strong>ConsultantName</strong></h4>
                    <input type="text" class="form-control" name="ConsultantName" id="ConsultantName" 
                      autocomplete="off" >
                    <h4><strong>Location</strong></h4>
                    <input type="text" class="form-control" name="Location" id="Location"
                        autocomplete="off" placeholder="Enter Location">
                    <h4><strong>Job Role</strong></h4>
                    <input type="text" class="form-control" name="Job Role" id="JobRole"
                        autocomplete="off" placeholder="Ex Java Developer">
                    // Other Fields to input data
                    <button type="submit"</button>
                   </div>
                </form>