gpt4 book ai didi

javascript - 关于请求参数的命名/选择的困惑

转载 作者:太空宇宙 更新时间:2023-11-04 00:19:46 25 4
gpt4 key购买 nike

我很难理解请求参数的命名方式(例如 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');
});
}
});
};

目录.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 中解析。

<小时/>

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

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

如果您尝试使用此表单字段,更改它会产生影响。

关于javascript - 关于请求参数的命名/选择的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44876332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com