为什么我添加数据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);
})
}
});