错误:Route.post()需要回调函数,但得到了[对象未定义]

时间:2019-02-01 11:23:22

标签: node.js

我已经在node.js中创建了用于基本Crud操作的服务。但是,当我启动服务时,抛出Route.get()需要回调函数,但出现[object undefined]错误。我无法弄清楚错误在哪里。

这是我的代码。

型号: Agreement_master.js。

const mongoose = require('mongoose');


const agreementmaster = mongoose.Schema({


    ClientId:{type:Number},
    UserId:{type:Number},
    StartDate:{type:Date},


});

 var Agreement = module.exports = 
mongoose.model('Agreement',agreementmaster);


 module.exports.addmAgreement=function (data,callback){

    data.save(callback);

 }

 module.exports.getallmAgreement= function (data,callback){
    var query = {status:true};
    Agreement.find(query, callback);
 }

路线: Agreement_master.js

var express = require('express'),
    router = express.Router(),
    magreement = require('../controller/agreement_master');

router.post('/addmAgreement', magreement.addmAgreement);


module.exports = router; 

控制器: Agreement_master.js

const Agreement= require('../models/agreement_master');


exports.addmAgreement = function (req, res){

    var data = JSON.parse(JSON.stringify(req.body));
    var agreement = new Agreement(data); 
    agreement.ClientId = req.body.ClientId;
    agreement.UserId= req.body.UserId;
    agreement.StartDate=new Date();

    Agreement.addmAgreement(agreement, function (err, obj) {
       if (err) return res.json({err, status:'error'});
       if(obj){
           return res.json({
               message:'agreement added',

           });
       }
   });
};

index.js

const express = require('express');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const expressValidator = require('express-validator');

const cookieParser = require('cookie-parser');

 mongoose.connect('mongodb://local/local-host')

const app = express();

错误出现在下面这一行:

const agreement = require('./routes/agreement_master');
app.use(morgan('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use(expressValidator());
app.use(cookieParser());

//在此处添加路线

app.use('/agreement', agreement);
app.set('port', (process.env.PORT || 3001));
    var server = app.listen(app.get('port'), function(){
    console.log('Server Started on port ' + app.get('port'));
    });




    module.exports = app;

2 个答案:

答案 0 :(得分:1)

user module.exports而不是导出。 出口不出口任何东西,require()不会使用它。

module.exports.addmAgreement = function (req, res){

    var data = JSON.parse(JSON.stringify(req.body));
    var agreement = new Agreement(data); 
    agreement.ClientId = req.body.ClientId;
    agreement.UserId= req.body.UserId;
    agreement.StartDate=new Date();

    Agreement.addmAgreement(agreement, function (err, obj) {
       if (err) return res.json({err, status:'error'});
       if(obj){
           return res.json({
               message:'agreement added',

           });
       }
   });
};

答案 1 :(得分:0)

尝试

router.post('/addmAgreement', function(req, res){
   magreement.addmAgreement
});  

代替

router.post('/addmAgreement', magreement.addmAgreement);