2017-07-03 70 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