我的代码:
app.js
var express = require("express")
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()
var bodyParser = require('body-parser');
mongoose.connect('mongodb://localhost:27017/imooc_projects')
app.set('views', './views/pages')
app.set('view engine', 'jade')
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'bower_components')))
app.listen(port)
console.log('imooc 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: 'imooc 首页',
movies: movies
})
})
})
//detail page
app.get('/movie/:id', function (req, res) {
var id = req.params.id
console.log("putain")
console.log(id)
Movie.findById(id, function(err, movie) {
console.log(movie)
res.render('detail', {
title: 'imooc' + movie.title,
movie: movie
})
})
})
//admin page
app.get('/admin/movie', function (req, res) {
res.render('admin', {
title: 'imooc 后台录入页',
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: 'imooc 后台更新页',
movie: movie
})
})
}
})
//admin post movie
app.post('/admin/movie/new', function(req, res) {
var id = req.body.movie._id
var movieObj = req.body.movie
var _movie
console.log(id)
if (id !== undefined) {
Movie.findById(id, function(err, movie) {
if (err) {
console.log(err)
}
console.log("alkfoaj1")
_movie = _.extend(movie, movieObj)
//console.log(movie)
//console.log(movieObj)
//console.log(_movie)
//console.log("alkfoaj2")
_movie.save(function(err, movie) {
if (err) {
console.log(err)
}
//console.log("alkfoaj3")
res.redirect('/movie/' + movie._id)
})
})
}
else {
console.log("else")
_movie = new Movie({
director: movieObj.director,
title: movieObj.title,
country: movieObj.country,
language: movieObj.language,
year: movieObj.year,
poster: movieObj.poster,
summary: movieObj.summary,
flash: movieObj.flash
})
_movie.save(function(err, movie) {
if (err) {
console.log(err)
}
//console.log(movie._id + "a")
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: 'imooc 列表页',
movies: movies
})
})
})
**movie.js**
var mongoose =require ('mongoose')
var MovieSchema = new mongoose.Schema({
director: String,
title: String,
language: String,
country: String,
summary: String,
flash: String,
poster: String,
year: Number,
meta:{
createAt:{
type: Date,
default: Date.now()
},
updateAt:{
type: Date,
default: Date.now()
}
}
})
MovieSchema.pre('save',function(next) {
if(this.isNew){
this.meta.createAt = this.meta.updateAt = Date.now();
}
else{
this.meta.updateAt = Date.now()
}
next()
//console.log("aa")
})
MovieSchema.statics = {
fetch: function(cb){
return this
.find({})
.sort('meta.updateAt')
.exec(cb)
},
findById: function(id, cb){
//console.log("aaa")
return this
.findOne({_id: id})`enter code here`
.exec(cb)
}
}
module.exports = MovieSchema
在这里输入代码
admin.jade
extend ../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.col-sm-10.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.col-sm-10.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.col-sm-10.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.col-sm-10.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.col-sm-10.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.col-sm-10.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.col-sm-10.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.col-sm-10.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") 录入
我收到此错误:TypeError: cannot read property ‘title’ of undefined
。
但是当我尝试打印movie
对象时,它会给我这样的错误:
这意味着: enter image description here
这部分运行了两次。 那真是怪了。 je sais pas pourquoi