混淆命名/select请求参数
我有困难试图了解如何请求参数命名(例如req.params.id,req.body.authorid等),以及他们如何与其他variables相关。
他们可以分配任何随机的名字? (例如req.params.x,req.body.y)
对于author_delete_get
函数中的以下代码,似乎只接受req.params.id
。 把它req.params.x
例如req.params.x
将会导致错误。
至于author_delete_post
函数,将req.body.authorid
改为例如req.body.authorids
似乎不会影响function。
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
在req.params.id
, .id
部分来自:id
部分:
router.get('/author/:id/delete', author_controller.author_delete_get);
从与此请求匹配的url中parsing出来。
在req.body.authorid
, req.body.authorid
部分来自POST请求提交的表单数据,可能来自表单的这一部分:
input#authorid.form-control(name='authorid', required='true', value=author._id )
如果您正在尝试使用此表单域,更改它将会影响事物。