猿问

TypeError: Cannot read property 'movie' of undefined

node+mongodb 建站攻略(一期)

在admin进行录入的时候发生报错如标题,代码如下:

admin.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-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="inputDoctor") 电影导演
          .col-sm-10
            input#inputDoctor.form-control(type="text", name="movie[doctor]", value="#{movie.doctor}")
        .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="inputFlash") 片源地址
          .col-sm-10
            input#inputFlash.form-control(type="text", name="movie[flash]", value="#{movie.flash}")
        .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
            textarea#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 bodyParser = require('body-parser')
var path = require('path')
var mongoose = require('mongoose')
var _ = require('underscore')
var Movie = require('./models/movie')
var port = process.env.NODE_ENV || 3000
var app = express()

app.set('views', './views/pages')
app.set('view engine', 'jade')
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname, 'bower_components')))
app.listen(port)

console.log('service started on port:' + port)

// admin post movie
app.post('/admin/movie/new', function(res, req){
  var id = req.body.movie._id
  var movieObj= req.body.movie
  var _movie

  if (id !== 'undefined') {
    Movie.findById(id, function(err, movie) {
      if (err) {
        console.log(err)
      }

      _movie = _.extend(movie, movieObj)
      _movie.save(function(err, movie) {
        if(err){
          console.log(err)
        }

        res.redirect('/movie/' + movie._id)
      })
    })
  }
  else {
    _movie = new Movie({
      doctor: movieObj.doctor,
      title: movieObj.title,
      country: movieObj.country,
      language: movieObj.language,
      year: movieObj.year,
      poster: movieObj.poster,
      summary: movieObj.summary,
      falsh: movieObj.falsh
    })

    _movie.save(function(err, movie) {
      if(err) {
        console.log(err)
      }

      res.redirect('/movie/' + movie._id)
    })
  }
})

已经把bodyparser单独安装在本地目录下,在运行时,post数据到地址“/admin/movie/new”,获取不到数据,req是有数据的,但是req.body为undefined不知道是哪里除了问题!!!

smileyby
浏览 679回答 1
1回答
随时随地看视频慕课网APP

相关分类

Node.js
我要回答