错误:Route.post()需要回调函数,但有一个[object Undefined] [英] Error: Route.post() requires callback functions but got a [object Undefined]

查看:230
本文介绍了错误:Route.post()需要回调函数,但有一个[object Undefined]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个回调函数,不知道为什么它不起作用...我有适当的用户系统,实际上我只是复制了该系统.不知道为什么一个有效,而另一个无效.我已经省略了不相关的代码,并且出于懒惰的目的将作业包含在用户模型文件中.

Has a callback don't know why this isn't working...I have working user system in place and I essentially just copied that system. Don't know why one works and the other doesn't. I've omitted the irrelevant code and included the jobs in the users model file out of laziness.

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

Error: Route.post() requires callback functions but got a [object Undefined] It looks like this line app.post('/postjob', jobs.createJob); triggers the error.

[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);

推荐答案

我认为您的 jobs_controller.js 中存在问题.尝试替换以下代码

I think the problem exists in your jobs_controller.js. Try replacing the below code

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');
            }
        });

    };

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

and also make sure you are sending the response instead of putting alert.

这篇关于错误:Route.post()需要回调函数,但有一个[object Undefined]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆