0
votes

I am trying to create a post method in order to store data into mongoDb but a type error occurs when i send a post request through postman

I have updated packages through npm but error remained same

TypeError: Cannot read property 'Customer_id' of undefined at exports.create_customer (C:\Users\jawad\Desktop\nodejspractise\controller\admin.controll.js:64:31) at Layer.handle [as handle_request] (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\layer.js:95:5) at next (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\route.js:137:13) at Route.dispatch (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\route.js:112:3) at Layer.handle [as handle_request] (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\layer.js:95:5) at C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:281:22 at Function.process_params (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:275:10) at Function.handle (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:174:3) at router (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:47:12) at Layer.handle [as handle_request] (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\layer.js:95:5) at trim_prefix (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:317:13) at C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:284:7 at Function.process_params (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:335:12) at next (C:\Users\jawad\Desktop\nodejspractise\node_modules\express\lib\router\index.js:275:10) at jsonParser (C:\Users\jawad\Desktop\nodejspractise\node_modules\body-parser\lib\types\json.js:101:7)

here is app.js file

const express= require('express');
    const body_parser=require('body-parser');
    const app=express();
    const mongo=require('mongoose');

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


    const admin_route=require('./routes/admin.routes');
    app.use('/admin',admin_route);
    mongo.connect("mongodb://localhost/DcPrimeDb", 
        {useNewUrlParser: true, useUnifiedTopology: true } );
    mongo.Promise = global.Promise;
    let db = mongo.connection;
    db.on('error', console.error.bind(console, 'MongoDB connection error:'));


    const port=8080;
    app.listen(port,()=>
        {

          console.log("Server is up "+port);
        });

customer.model.js file

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

let Customer_schema=new Schema(
    {

        Customer_id:{
            type:Number,
            required:true
        }, 
        Password:{type:String,
            required:true,
            max:100},

        Name:{type:String,
            required:true,
            max:100},

        Email:{type:String,
            required:true,
            max:200},

        Occupation:{type:String,
            required:true,
            max:30},

        Gender:{type:String,
            required:true,
            max:1,
            min:1},

        Dob:{type:Date,required:true,},
         Address:{type:String,required:true,max:500},
          City:{type:String,required:true,max:40},
           Country:{type:String,required:true,max:100}
    }

);
module.exports=mongoose.model('Customer',Customer_schema,'Customers');

admin.controll.js file:

const Customer=require('..//model/customer.model');
const employee=require('..//model/employee.model');
const changerequest=require('..//model/ChangeRequest.model');
const joi=require('joi');
const mongo=require('mongoose');
let db=mongo.Connection;




exports.create_customer=function (res,req) 
{
    let  customer= new Customer(
    {

         Customer_id:req.body.Customer_id,

        Password:req.body.Password, 
        Name:req.body.Name, 
        Email:req.body.Email, 
        Occupation:req.body.Occupation, 
        Gender:req.body.Gender, 
        Dob:req.body.Dob ,
         Address:req.body.Address,
          City:req.body.City,
           Country:req.body.Country
    })

    customer.save((err)=>
    {
        if (err)
        {
            return next(err);
        }
        res.send("Customer  Sucessfull Created");

    }
    )

}

admin.routes.js file:

   const  express=require('express');
    const router=express.Router();
    const body_parser=require('body-parser');
    const app=express();
    app.use(body_parser.json());
    app.use(body_parser.urlencoded({extended:true}));
    const admin_controll=require('../controller/admin.controll');
    router.post('/createCustomer',admin_controll.create_customer);

    // router.get('/test',product_controll.test);
    module.exports=router;
1
Please make sure to use Content-Type:application/json in header in post manSonu Sharma
Also the package joi has been depricated as per npmjs.com/package/joiSonu Sharma
Did my answer resolve the problem? If so can you please mark it as answer? stackoverflow.com/help/someone-answersSuleymanSah
thanks for help everyoneJawad Javaid

1 Answers

0
votes

You are here exports.create_customer=function (res,req) not specifying correct order. It must be exports.create_customer=function (req,res)