我的post页为什么在路由的时候告诉我
TypeError: Cannot read property '_id' of undefined
at app.get.Movie.fetch.res.render.title (D:\Mooc\app.js:80:25)
请高手指教啊
我的post页为什么在路由的时候告诉我
TypeError: Cannot read property '_id' of undefined
at app.get.Movie.fetch.res.render.title (D:\Mooc\app.js:80:25)
请高手指教啊
2014-08-29
var express = require('express') var bodyParser = require('body-parser') var path = require('path') var mongoose = require('mongoose') var _ = require('underscore') var Movie = require('./models/movie') var port = process.env.PORT || 3000 var app = express() mongoose.connect('mongodb://localhost/imooc') app.set('views', './views/pages') app.set('view engine', 'jade') app.use(bodyParser.urlencoded({ extended: true })) // admin post movie app.post('/admin/movie/new', function(res,req) { var id = req.body.movie._id -----错在这行 var movieObj = req.body.movie var _movie Jade 代码: extends ../layout block content .container .row form.form-horizontal(method="post", action="/admin/movie/new") input(type="hidden", name="movie[_id]", value="#{movie._id}") .form-group label.col-sm-3.control-label(for="inputTitle") Name .col-sm-8 input#inputTitle.form-control(type="text", name="movie[title]", value="#{movie.title}")
为什么我改了以后还是报错,错误是“Cannot read property 'movie' of undefined”。应该还是body-parser解析的问题,但查了半天都不明白错在哪里。还能如何修改
终于搞明白了,就是body-parser的问题,晕晕乎乎的看了半天的GitHub上关于express/bodyparser的说明,并反复试验,最终在app.js中使用下面的分析器可以继续了app.use(bodyParser.urlencoded({ extended: true })),谢谢大家
我已经确认了,是和body-parser模块有关系,因为我使用了新版本的git所以使用parser的方法可能有问题,我原先使用bodyParser.JOSN()是没法解析request页的,但是我试了所有的选项只有bodyParser.urlencoded({ extended: false })可以解析成
{}
{}
{}
{}
{}
{}
{ 'movie[_id]': 'undefined',
'movie[Title]': 'asdfasdf',
'movie[Director]': 'asdf',
'movie[Country]': 'asdf',
'movie[Language]': 'asdf',
'movie[Poster]': 'asdf',
'movie[Year]': 'asdf',
'movie[Summary]': 'asdf' }
所以在使用var id = req.body.movie._id时就会报错。
请问老师怎么才能正常解析呢?
而且,我觉得这段代码的意思是从admin.jade中取名字声明为name="movie[_id]"控件的值,但是我试了一下,req.body.movie可以取到,但是下面的各个属性都取不到,不知道是为什么啊?求解答
我的admin.jade和app.js如下:
extends ../layout
block content
.container
.row
form.form-horizontal(method="post", action="/admin/movie/new")
input(type="hidden", name="movie[_id]", value="#{movie._id}")
.form-group
label.col-sm-2.control-label(for="inputTitle") 电影名称
.col-sm-10
input#inputTitle.form-control(type="text", name="movie[Title]", value="#{movie.Title}")
.form-group
label.col-sm-2.control-label(for="inputDirector") 电影导演
.col-sm-10
input#inputDirector.form-control(type="text", name="movie[Director]", value="#{movie.Director}")
.form-group
label.col-sm-2.control-label(for="inputCountry") 国家
.col-sm-10
input#inputCountry.form-control(type="text", name="movie[Country]", value="#{movie.Country}")
.form-group
label.col-sm-2.control-label(for="inputLanguage") 语种
.col-sm-10
input#inputLanguage.form-control(type="text", name="movie[Language]", value="#{movie.Language}")
.form-group
label.col-sm-2.control-label(for="inputPoster") 海报地址
.col-sm-10
input#inputPoster.form-control(type="text", name="movie[Poster]", value="#{movie.Poster}")
.form-group
label.col-sm-2.control-label(for="inputYear") 上映年份
.col-sm-10
input#inputYear.form-control(type="text", name="movie[Year]", value="#{movie.Year}")
.form-group
label.col-sm-2.control-label(for="inputSummary") 简介
.col-sm-10
input#inputSummary.form-control(type="text", name="movie[Summary]", value="#{movie.Summary}")
.form-group
.col-sm-offset-2.col-sm-10
button.btn.btn-default(type="submit") 登录
----- app.js
var express = require('express')
var path = require('path')
var mongoose = require('mongoose')
var _ = require('underscore')
var Movie = require('./modles/movie')
var bodyParser = require('body-parser')
var port = process.env.PORT || 3000
var app = express()
mongoose.connect('mongodb://localhost/MoocDB')
app.set('views', './views/pages')
app.set('view engine', 'jade')
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'bower_components')))
app.listen(port)
console.log('Mooc started on port ' + port)
// index page
app.get('/', function(req,res) {
Movie.fetch(function(err, movies) {
if(err) {
console.log(err)
}
res.render('index', {
title: 'Mooc 首页',
movies: movies
})
})
})
// detail page
app.get('/movie/:id', function(req,res) {
var id = req.params.id
Movie.findById(id, function(err, movie) {
if(err) {
console.log(err)
}
res.render('detail', {
title: 'Mooc 详情页' + movie.Title,
movie: movie
})
})
})
// admin page
app.get('/admin/movie', function(req,res) {
res.render('admin', {
title: 'Mooc 后台录入页',
movie: {
Director: '',
Country: '',
Title: '',
Year: '',
Poster: '',
Language: '',
Flash: '',
Summary: ''
}
})
})
// admin update movie
app.get('/admin/update/:id', function(req, res) {
var id = req.params.id
if(id) {
Movie.findById(id, function(err, movie) {
res.render('admin', {
Title: 'Mooc 后台录入页',
movie: movie
})
})
}
})
// admin post movie
app.post('/admin/movie/new', function(req, res) {
var movieObj = req.body.movie
var id = req.body.movie._id
var _movie
if(id !== 'undefined') {
Movie.findById(id, function(err, movie) {
if(err) {
console.log(err)
}
_movie = _.extend(movie, movieObj)
console.log(_movie.Title)
//-_movie.save(function(err, movie) {
//- if(err) {
//- console.log(err)
//- }
//- res.redirect('/movie/' + movie._id)
//-})
})
}
else {
_movie = new Movie({
Title: movieObj.Title,
Dirctor: movieObj.Dirctor,
Country: movieObj.Country,
Language: movieObj.Language,
Year: movieObj.year,
Poster: movieObj.Poster,
Summary: movieObj.Summary,
Flash: movieObj.Flash
})
console.log(_movie.Title)
//-_movie.save(function(err, movie) {
//- if(err) {
//- console.log(err)
//- }
//- res.redirect('/movie/' + movie._id)
//-})
}
})
// list page
app.get('/admin/list', function(req,res) {
Movie.fetch(function(err, movies) {
if(err) {
console.log(err)
}
res.render('list', {
title: 'Mooc 列表页',
movies: movies
})
})
})
举报