movies is not defined,有人知道什么原因吗
求!!!!!
求!!!!!
2016-08-25
var express = require("express"); var path = require("path"); var mongoose = require("mongoose"); var movie = require("./models/movie"); var _ = require("underscore"); var bodyParser = require('body-parser'); var port = process.env.PORT || 3000; var app = express(); mongoose.connect("mongodb://localhost/imooc-projects"); // 链接数据库 app.set("views", "./views/pages"); app.set("view engine", "jade"); app.use(bodyParser.urlencoded()); 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; Movie.findById(id, function(err, movie) { res.render("detail", { title: "imooc" + movie.title, movie: movie }) }) }) // 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 page app.get("/admin/movie", function(req, res) { res.render("admin", { title: "imooc 后台录入页", movie: { title: '', doctor: '', country: '', year: '', poster: '', flash: '', summary: '', language: '' } }) }) // 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(req.body.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({ title: movieObj.title, doctor: movieObj.doctor, country: movieObj.country, language: movieObj.language, poster: movieObj.poster, flash: movieObj.flash, year: movieObj.year, summary: movieObj.summary }); _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: "imooc 列表页", movies: movies }) }) })
也是Movie is not defined报错;
找了一下午原因。。。。。。
举报