mongodb的_id在入口文件获取不到?

ritsu
  • 746

入口文件部分代码:

   app.post("/admin/movie/new", function (req, res) {
var id=req.body.movie._id;
var movieObj = req.body.movie;
var _movie;

Jade 文件部分代码:


 extends ../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") movieName
           .col-sm-10
              input#inputTitle.form-control(type="text",name="movie[title]",value="#{movie.title}")
        .form-group
           label.col-sm-2.control-label(for="inputDoctor") movieDoctor
           .col-sm-10
              input#inputDoctor.form-control(type="text",name="movie[doctor]",value="#{movie.doctor}")

           .col-sm-offset-2.col-sm-10
               button.btn.btn-danger(type="submit") 录入
     
     
     我看到网上说是因为没有引用bodyParser模块的问题,但是我引用了,
     代码如下:
        var bodyParser = require("body-parser");
        app.use(bodyParser.urlencoded({extended:true}));

app.use(express.static("public"));

               
               
               
       结果还是报错:
       
         TypeError: Cannot read property '_id' of undefined

at C:\Users\Administrator\Desktop\项目\Node\app.js:79:28
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:277:22
at Function.process_params (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:271:10)
at serveStatic (C:\Users\Administrator\Desktop\项目\Node\node_modules\serve-static\index.js:74:16)
at Layer.handle [as handle_request] (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:312:13)
at C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:280:7
at Function.process_params (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:330:12)
at next (C:\Users\Administrator\Desktop\项目\Node\node_modules\express\lib\router\index.js:271:10)
at C:\Users\Administrator\Desktop\项目\Node\node_modules\body-parser\lib\read.js:129:5
at invokeCallback (C:\Users\Administrator\Desktop\项目\Node\node_modules\raw-body\index.js:262:16)

回复
阅读 2.4k
2 个回答

这个报错的意思是没有拿到req.body.movie,所以是Cannot read property '_id' of undefined

你查看一下,你是否在服务器端使用了解析表单数据的中间件,例如:body-parser,
还有你是否使用正确,同时把相关配置代码app.js发一下

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏