【问题标题】:Error: Route.post() requires callback functions but got a [object Undefined]错误:Route.post() 需要回调函数,但得到了 [object Undefined]
【发布时间】:2015-12-01 12:25:27
【问题描述】:

有一个回调不知道为什么这不起作用...我有工作的用户系统,我基本上只是复制了那个系统。不知道为什么一个有效而另一个无效。出于懒惰,我省略了不相关的代码并将作业包含在用户模型文件中。

错误:Route.post() 需要回调函数,但得到了 [object Undefined] 它看起来像这条线 app.post('/postjob', jobs.createJob);触发错误。

[index.js]
mongoose.connect(uri);
require('./models/users_model.js');
require('./routes/routes.js')(app);

[routes.js]
module.exports = function(app){

    var jobs = require('../controllers/jobs_controller.js');

    app.all('/postjob', function(req, res){
        if (typeof req.session !== 'undefined' && req.session.user) {
            res.redirect('/');
        }
        res.render('pages/login', {msg:req.session.msg});
    });

    app.post('/postjob', jobs.createJob);
}



[jobs_controller.js]
var mongoose = require('mongoose'),
    Job = mongoose.model('Job');

module.exports = function(app){

    exports.createJob = function(req, res){

        var job = new Job({created: Date.now});
        job.set('title', req.body.title);
        job.set('description', req.body.description);
        job.set('hours', req.body.hours);
        job.save(function(err){
            if (err) {
                alert(err);
            }
            else {
                alert('Job Posted Succesfully');
            }
        });

    }

};

[users_models.js]
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;

var UserSchema = new Schema ({
    username: { type: String, unique: true },
    email: String,
    color: String,
    hashed_password: String
});

mongoose.model('User', UserSchema);

var JobSchema = new Schema ({
    created: Date,
    postedBy: String,
    title: String,
    description: String,
    hours: String,
    applicants: String,
    closed: Date
});

mongoose.model('Job', JobSchema);

【问题讨论】:

  • 请同时提供您遇到的错误

标签: mongodb express mongoose


【解决方案1】:

我认为问题存在于您的jobs_controller.js。尝试替换下面的代码

module.exports = function(app){

    exports.createJob = function(req, res){

        var job = new Job({created: Date.now});
        job.set('title', req.body.title);
        job.set('description', req.body.description);
        job.set('hours', req.body.hours);
        job.save(function(err){
            if (err) {
                alert(err);
            }
            else {
                alert('Job Posted Succesfully');
            }
        });

    }

};

exports.createJob = function(req, res){

        var job = new Job({created: Date.now});
        job.set('title', req.body.title);
        job.set('description', req.body.description);
        job.set('hours', req.body.hours);
        job.save(function(err){
            if (err) {
                alert(err);
            }
            else {
                alert('Job Posted Succesfully');
            }
        });

    };

并确保您发送的是响应而不是发出警报。

【讨论】:

  • 谢谢 Rudra,它现在可以编译并加载到 localhost..我将不得不在未来的日子里测试功能。
猜你喜欢
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多