為什么我添加數據post成功了,但是轉到詳情頁它會報錯,沒有數據顯示出來,而且數據庫沒有查到數據
//detail page
app.get("/movie/:id", function (req, res) {
? ? ? ?var id = req.params.id;
//這個id請求了2次,第二次就是偏偏unddefin,求解析
? ?console.log(req.params, id, "MOVIE/ID");
? ?Movie.findById(id, function (err, movie) {
? ? ? ?console.log(err,movie,"我我我");
? ? ? ?res.render("detail", {
? ? ? ? ? ?title: "imooc",
? ? ? ? ? ?movie: movie
? ? ? ?})
? ?})
});
//提交表單 admin post movie
app.post("/admin/movie/new", function (req, res) {
? ?var body = req.body;
? ?var id = req.body.movie._id;
? ?var movieObj = req.body.movie;
? ?console.log(body, id, movieObj, "我愛撫你");
? ?var _movie;
? ?if (id !== 'undefined') {
? ? ? ?//如果id已經存在,就用一個新的對象來替換
? ? ? ?Movie.findById(id, function (err, movie) {
? ? ? ? ? ?if (err) {
? ? ? ? ? ? ? ?console.log(err);
? ? ? ? ? ?}
? ? ? ? ? ?_movie = underscore.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,
? ? ? ? ? ?flash: movieObj.flash
? ? ? ?});
? ? ? ?console.log(_movie);
? ? ? ?_movie.save(function (err, movie) {
? ? ? ? ? ?if (err) {
? ? ? ? ? ? ? ?console.log(err);
? ? ? ? ? ?}
? ? ? ? ? ?console.log("我愛你2 ?是")
? ? ? ? ? ?//保存成功 頁面跳轉到詳情頁面;
? ? ? ? ? ?res.redirect("/movie/" + movie._id);
? ? ? ?})
? ?}
});