2017-07-03 76 views
1

我在试图理解请求参数如何命名(例如req.params.id,req.body.authorid等)以及它们如何与其他变量相关时遇到困难。关于命名/选择请求参数的困惑

他们可以分配任何随机名称吗? (例如req.params.x,req.body.y)

对于author_delete_get函数中的以下代码,似乎只接受req.params.id。将其更改为例如req.params.x将导致错误。

至于author_delete_post功能,将req.body.authorid更改为例如req.body.authorids似乎不影响功能。

authorController.js

// Display Author delete form on GET 
exports.author_delete_get = function(req, res, next) { 
    async.parallel({ 
     author: function(callback) { 
      Author.findById(req.params.id).exec(callback); 
     }, 
     authors_books: function(callback) { 
     Book.find({ 'author': req.params.id }).exec(callback); 
     }, 
    }, function(err, results) { 
     if (err) { return next(err); } 
     //Successful, so render 
     res.render('author_delete', { title: 'Delete Author', author: results.author, author_books: results.authors_books }); 
    }); 
}; 

// Handle Author delete on POST 
exports.author_delete_post = function(req, res, next) { 
    req.checkBody('authorid', 'Author id must exist').notEmpty(); 
    async.parallel({ 
     author: function(callback) { 
      Author.findById(req.body.authorid).exec(callback); 
     }, 
     authors_books: function(callback) { 
     Book.find({ 'authors': req.body.authorid },'title summary').exec(callback); 
     }, 
    }, function(err, results) { 
     if (err) { return next(err); } 
     //Success 
     if (results.authors_books>0) { 
      //Author has books. Render in same way as for GET route. 
      res.render('author_delete', { title: 'Delete Author', author: results.author, author_books: results.authors_books }); 
      return; 
     } 
     else { 
     //Author has no books. Delete object and redirect to the list of authors. 
      Author.findByIdAndRemove(req.body.authorid, function deleteAuthor(err) { 
       if (err) { return next(err); } 
       //Success - got to author list 
       res.redirect('/catalog/authors'); 
      }); 
     } 
    }); 
}; 

catalog.js

/* GET request to delete Author. */ 
router.get('/author/:id/delete', author_controller.author_delete_get); 

// POST request to delete Author 
router.post('/author/:id/delete', author_controller.author_delete_post); 

author_detail.pug

extends layout 

block content 

    h1 Author: #{author.name} 
    p #{author.date_of_birth_formatted} - #{author.date_of_death_formatted} 

    div(style='margin-left:20px;margin-top:20px') 

    h4 Books 

    dl 
    each book in author_books 
     dt 
     a(href=book.url) #{book.title} 
     dd #{book.summary} 
     br 
    else 
     p This author has no books. 
    br 
    p 
     a(href=author.url+'/delete') Delete author 

author_delete.pug

extends layout 

block content 
    h1 #{title}: #{author.name} 
    p= author.lifespan 

    if author_books.length 

    p #[strong Delete the following books before attempting to delete this author.] 

    div(style='margin-left:20px;margin-top:20px') 

     h4 Books 

     dl 
     each book in author_books 
     dt 
      a(href=book.url) #{book.title} 
     dd #{book.summary} 

    else 
    p Do you really want to delete this Author? 

    form(method='POST' action='') 
     div.form-group 
     input#authorid.form-control(name='authorid', required='true', value=author._id) 

     button.btn.btn-primary(type='submit') Delete 

回答

1

req.params.id中,.id部分来源于此:id部分:

router.get('/author/:id/delete', author_controller.author_delete_get); 

这是从这个请求匹配的URL解析。


req.body.authorid中,.authorid部分来自与POST请求提交的表单数据,它可能来自你的表格中的这一部分:

input#authorid.form-control(name='authorid', required='true', value=author._id) 

更改它会影响的事情,如果你想使用这个表单字段。

+0

谢谢@ jfriend00的澄清! 对于POST请求,它实际上是参照'input#authorid'还是'name ='authorid''? ,因为当我将'req.body.authorid'改为'req.body.authorids'时,删除功能仍然正常工作,这令我感到困惑 –

+0

@JianHaoTan - 假设您使用body-parser模块和表单是由浏览器以URL编码格式提交的,我相信浏览器会使用表单控件的'name = xxx'属性在编码体中建立'xxx = yyy'对,并将其解析为'req。 body.xxx = yyy'。 – jfriend00