我在尝试理解请求参数的命名方式时遇到了困难(例如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
答案 0 :(得分:1)
在req.params.id
中,.id
部分来自:id
部分:
router.get('/author/:id/delete', author_controller.author_delete_get);
从与此请求匹配的网址解析。
在req.body.authorid
中,.authorid
部分来自提交的表单数据和POST请求,它可能来自您表单的这一部分:
input#authorid.form-control(name='authorid', required='true', value=author._id )
如果您尝试使用此表单字段,更改它会影响事情。