.post需要回调函数但得到一个[对象未定义]

时间:2017-10-16 09:36:56

标签: node.js mongodb express mongoose

我正在尝试使用路线,但它会引发错误。

没有路线工作,所以路线一定有问题, 你介意帮帮我吗?

index.js(路线文件夹)

 module.exports = function(events){
                var mongoose = require('mongoose');
                var loadSchema = require('../schemas/index');
                var functions={};
               // save function
               functions.saveEvent = function (req, res) {
        //schema loading
            new loadSchema({
                name:req.body.organizer,
                email:req.body.email,
                address:req.body.address,
                street:req.body.street,
                price:req.body.price,
                category:req.body.category,
                otherInfo:req.body.otherInfo
            }).save(function(error,data){
                if(error)
                    res.json(error);
                else
                    res.send("Event Saved");
            });
        };
          return functions;
      }

app.js

app.post('/addEvent',routes.saveEvent); // addEvent is the action of form

index.js(架构文件夹)

var mongoose = require('mongoose');

    module.exports = mongoose.model('user', {
        name: Number,
        email: String,
        favoriteBook: String,
        password: String,
        confimrPassword: String
        });


    module.exports=mongoose.model('event',{
        organizer:String,
        email:String,
        address:String,
        street:String,
        category:String,
        price:String,
        otherInfo:String
    })
  

错误:.post需要回调函数但得到[object Undefined]

1 个答案:

答案 0 :(得分:0)

index.js(routes文件夹)公开了一个返回其他中间件函数的函数。如果要访问中间件函数,则需要在索引中调用导出的函数。 JS:

更改app.post('/addEvent',routes.saveEvent); 至 app.post( '/的addEvent',路线()saveEvent。);