server js code
const express=require('express');
const app = new express();
app.use(express.json())// will convert string into obj
const employeeRouter=require('../controller/employee-controller');
app.use(employeeRouter);
require('../models/db')
const hbs=require('hbs');
const path=require('path');
const publicPath=path.join(__dirname,'../public');
app.use(express.static(publicPath));
const Employee=require('../models/employee-model');
const viewsPath=path.join(__dirname,"../views");
app.set('views',viewsPath);
app.set('view engine','hbs');
app.listen(8000,()=>{
console.log("connect to server bro");
})
router file
const express=require('express');
const Employee=require('../models/employee-model');
const router=express.Router();
router.get('/',(req,res)=>{
res.send("home page");
})
router.get('/add',(req,res)=>{
res.render('index');
})
router.post('/employee',async(req,res)=>{
const employee=new Employee()
try{
await employee.save();
res.status(200).send("recorded successfully")
}
catch(err){
res.status(404).send(err)
}
// console.log(req.body);
})
module.exports=router;
mongoose model
const mongoose=require('mongoose');
const validator=require('validator');
const employeeSchema=new mongoose.Schema({
name:{
type:String,
required:true,
trim:true
},
email:{
type:String,
required:true,
trim:true,
unique:true,
validate(value){
if(!validator.isEmail(value)){
throw new Error ("Please enter correct email");
}
}
},
number:{
type:Number,
required:true,
unique:true,
validate(value){
if(value.length!=10){
throw new Error ("Please Enter 10 digit mobile number");
}
}
},
city:{
type:String,
required:true,
}
})
const Employee=mongoose.model('employees',employeeSchema);
module.exports=Employee;
error while posting data
{
"errors": {
"city": {
"name": "ValidatorError",
"message": "Path `city` is required.",
"properties": {
"message": "Path `city` is required.",
"type": "required",
"path": "city"
},
"kind": "required",
"path": "city"
},
"number": {
"name": "ValidatorError",
"message": "Path `number` is required.",
"properties": {
"message": "Path `number` is required.",
"type": "required",
"path": "number"
},
"kind": "required",
"path": "number"
},
"email": {
"name": "ValidatorError",
"message": "Path `email` is required.",
"properties": {
"message": "Path `email` is required.",
"type": "required",
"path": "email"
},
"kind": "required",
"path": "email"
},
"name": {
"name": "ValidatorError",
"message": "Path `name` is required.",
"properties": {
"message": "Path `name` is required.",
"type": "required",
"path": "name"
},
"kind": "required",
"path": "name"
}
},
"_message": "employees validation failed",
"name": "ValidationError",
"message": "employees validation failed: city: Path `city` is required., number: Path `number` is required., email: Path `email` is required., name: Path `name` is required."
}
data i am posting
{
"name":"mahir jain",
"email":"mahirjain@gmail.com",
"number":1234567890,
"city":"mumbai"
}
need serious help cause i am stuck at this issue past 15 days