請問添加頁的id是怎么來的?
在下面,錄入頁面的代碼,?input(type='hidden', name="movie[_id]", value="#{movie._id}") 這是一個隱藏域,也就是說,我們根本不能給這個隱藏域輸入值,此時,提交按鈕的話,movie[_id]是無值的,但是在下面的路由中,卻有這個代碼:
var id = req.body.movie._id//這個movie._id是怎么來的
請問這個movie._id是怎么來的?
這是錄入頁面的代碼:
```
extends ../layout
block content
? .container
? ? ?.row
? ? ? ? form.form-horizontal(method="post", action="/admin/movie/new")
? ? ? ? ? ?input(type='hidden', name="movie[_id]", value="#{movie._id}")
? ? ? ? ? ?//使用hidden隱藏域,用來承載數據,類似elec
? ? ? ? ? ?.form-group
? ? ? ? ? ? ? label.col-sm-2.control-label(for="inputTitle") Name
? ? ? ? ? ? ? .col-sm-10
? ? ? ? ? ? ? ? ?input#inputTitle.form-control(type="text", name="movie[title]", value="#{movie.title}")
? ? ? ? ? ?.form-group
? ? ? ? ? ? ? label.col-sm-2.control-label(for="inputDirector") Director
? ? ? ? ? ? ? .col-sm-10
? ? ? ? ? ? ? ? ?input#inputDirector.form-control(type="text", name="movie[director]", value="#{movie.director}")
? ? ? ? ? ?.form-group
? ? ? ? ? ? ? label.col-sm-2.control-label(for="inputCountry") Country
? ? ? ? ? ? ? .col-sm-10
? ? ? ? ? ? ? ? ?input#inputCountry.form-control(type="text", name="movie[country]", value="#{movie.country}")
? ? ? ? ? ?.form-group
? ? ? ? ? ? ? label.col-sm-2.control-label(for="inputLang") Lang
? ? ? ? ? ? ? .col-sm-10
? ? ? ? ? ? ? ? ?input#inputLang.form-control(type="text", name="movie[lang]", value="#{movie.lang}")
? ? ? ? ? ?.form-group
? ? ? ? ? ? ? label.col-sm-2.control-label(for="inputPoster") Poster
? ? ? ? ? ? ? .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") Flash
? ? ? ? ? ? ? .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") Year
? ? ? ? ? ? ? .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") Summary
? ? ? ? ? ? ? .col-sm-10
? ? ? ? ? ? ? ? ?input#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") Submit
```
```
//錄入頁面的提交請求路由
app.post('/admin/movie/new', function(req,res){
? ?
? var id = req.body.movie._id//這個movie._id是怎么來的
? ?console.log("我是monie/new:" + JSON.stringify(req.body));
? //todo 疑問req.body.movie這里的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)
? ? ? ? ? ?}
? ? ? ? ? ?// 重定向,就是說,當點擊提交時,頁面由'/admin/movie/new'重定向為'/movie/' + movie._id
? ? ? ? ? ?res.redirect('/movie/' + movie._id)
? ? ? ? })
? ? ?})
? ? ?// res.json({success: "提交存儲開始,我是更新數據,即將進行重定向"})
? } else {//如果是新加的數據
? ? ?// res.json({success: "提交存儲開始,我是新加數據"})
? ? ?_movie = new Movie({
? ? ? ? director: movieObj.director,
? ? ? ? title: movieObj.title,
? ? ? ? country: movieObj.country,
? ? ? ? lang: movieObj.lang,
? ? ? ? year: movieObj.year,
? ? ? ? poster: movieObj.poster,
? ? ? ? summary: movieObj.summary,
? ? ? ? flash: movieObj.flash,
? ? ?})
? ? ?_movie.save(function(err, movie){
? ? ? ? if(err){
? ? ? ? ? ?console.log(err)
? ? ? ? }
? ? ? ? res.redirect('/movie/' + movie._id)
? ? ?})
? }
})
```
2016-09-29
同問,一直提示_id ?undefined