未定义模型属性的路由错误

时间:2014-07-11 01:10:20

标签: node.js express routing mongoose

我遇到了Express 4的路由问题,这与我为我的mongoose架构创建的属性有关。出于某种原因,当我尝试POST请求时,我收到一个引用错误,说明没有定义。我弄乱了我的路线请求吗?

错误:

ReferenceError: description is not defined
at Object.handle (/Users/user/Desktop/Projects/node/travelApp/app/routes.js:47:5)
at next_layer (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:103:13)
at Route.dispatch (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:107:5)
at c (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/index.js:195:24)
at Function.proto.process_params (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/index.js:251:12)
at next (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/index.js:189:19)
at next_layer (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:77:14)
at next_layer (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:81:14)
at next_layer (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:81:14)
at Route.dispatch (/Users/user/Desktop/Projects/node/travelApp/node_modules/express/lib/router/route.js:107:5)

destination.js

 var mongoose   =   require('mongoose');
var Schema      =   mongoose.Schema;

var DestinationSchema   =   new Schema({
    tite:   String,
    description:    String,
    topOne:     String,
    topTwo:     String,
    topThree:   String,
    topFour:    String,
    topFive:    String
});

module.exports  =   mongoose.model('Destination', DestinationSchema);

routes.js:

module.exports = function(app, passport, Destination) {



    app.get('/', function(req, res) {
        res.render('index.ejs')
    });

    app.get('/login', function(req, res) {

        res.render('login.ejs', {   message: req.flash('loginMessage')  });
    });

    app.post('/login', passport.authenticate('local-login', {
        successRedirect :   '/profile',
        failureRedirect :   '/login',
        failureFlash    :   true
    }));

    app.get('/signup', function(req, res) {
        res.render('signup.ejs', {  message: req.flash('signupMessage') });
    });

    app.post('/signup', passport.authenticate('local-signup', {
        successRedirect :   '/profile',
        failureRedirect :   '/signup',
        failureFlash    :   true
    }));

    app.get('/profile', isLoggedIn, function(req, res) {
        res.render('profile.ejs', {
            user    : req.user
        });
    });

    app.get('/destinations', isLoggedIn, function(req, res) {
                res.render('destinations.ejs', {
                user    :   req.user
                });
            });

    app.post('/destinations', function(req, res) {

                var destination = new Destination();
                destination.name = req.body.name,
                destination.description = req.body.description,
                description.topOne = req.body.topOne,
                description.topTwo = req.body.topTwo,
                description.topThree = req.body.topThree,
                description.topFour = req.body.topFour,
                description.topFive = req.body.topFive;

                destination.save(function(err){
                    if (err)
                        res.send(err);

                    res.json({ message: 'Destination created.'});
                });

            });

    app.get('/logout', function(req, res) {
        req.logout();
        res.redirect('/');
    });





};




function isLoggedIn(req, res, next) {

    if(req.isAuthenticated())
        return next();

    res.redirect('/');
}

1 个答案:

答案 0 :(得分:0)

destination.name = req.body.name,
destination.description = req.body.description,
description.topOne = req.body.topOne,
description.topTwo = req.body.topTwo,
description.topThree = req.body.topThree,
description.topFour = req.body.topFour,
description.topFive = req.body.topFive;

有些是destination,有些是description。根据您的上述代码,我认为他们都应该destination