NodeJs TypeError:无法读取未定义的属性“ Customer_id”

时间:2020-04-04 18:44:53

标签: node.js express

我试图创建一个post方法,以便将数据存储到mongoDb中,但是当我通过邮递员发送发布请求时,发生类型错误

我已经通过npm更新了程序包,但错误保持不变

TypeError:无法读取未定义的属性“ Customer_id” 在exports.create_customer(C:\ Users \ jawad \ Desktop \ nodejspractise \ controller \ admin.controll.js:64:31) 在Layer.handle [作为handle_request](C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ layer.js:95:5) 在下一个(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ route.js:137:13) 在Route.dispatch(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ route.js:112:3) 在Layer.handle [作为handle_request](C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ layer.js:95:5) 在C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:281:22 在Function.process_params(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:275:10) 在Function.handle(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:174:3) 在路由器上(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:47:12) 在Layer.handle [作为handle_request](C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ layer.js:95:5) 在trim_prefix(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:317:13) 在C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:284:7 在Function.process_params(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:335:12) 在下一个(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ express \ lib \ router \ index.js:275:10) 在jsonParser(C:\ Users \ jawad \ Desktop \ nodejspractise \ node_modules \ body-parser \ lib \ types \ json.js:101:7)

这是app.js文件

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文件

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文件:

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文件:

   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 个答案:

答案 0 :(得分:0)

您在exports.create_customer=function (res,req)上未指定正确的顺序。 它必须是exports.create_customer=function (req,res)