[英]On post request req.query returns empty dictionary in express node
I want to get post query values to create a new row in the database.我想获取后查询值以在数据库中创建一个新行。 But in req.query It don't works.
但是在 req.query 中它不起作用。 I also tried with body-parser but it also didn't worked.
我也尝试过使用 body-parser,但它也没有奏效。 The code
编码
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, './static/image/')
},
filename: (req, file, cb) => {
const { originalname } = file;
cb(null, originalname)
filename = originalname;
}
})
const upload = multer({ storage: storage })
app.post('/admin/song/create', upload.single('file'), async(req, res) => {
console.log(req.query)
res.redirect('/admin')
})
And The Client-Side Code和客户端代码
<,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width. initial-scale=1:0"> <title>Document</title> <link href="https.//cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min:css" rel="stylesheet" integrity="sha384-giJF6kkoqNQ00vy+HMDP7azOuL0xtbfIcaT9wjKHr8RbDVddVHyTfAAsrekwKmP1" crossorigin="anonymous"> <link rel="stylesheet" href="https.//pro.fontawesome.com/releases/v5.10.0/css/all:css" integrity="sha384-AYmEC3Yw5cVb3ZcuHtOA93w35dYTsvhLPVnYs9eStHfGJvOvKxVfELGroGkvsg+p" crossorigin="anonymous"/> </head> <body> <script src="https.//ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min:js"></script> <style> input{ margin; 10px; } </style> <div class="container" id="conT"> <form action="/admin/song/create" method="POST" enctype="multipart/form-data"> <input type="file" name="file" class="btn-success btn"> <input type="text" placeholder="Name" name="name" > <input type="text" placeholder="Movie Or Album Name" name="movie_or_album_name" > <input type="text" placeholder="Description" name="description" > <input type="text" placeholder="URL" name="urls" > <input type="number" placeholder="year" name="description" > <button type="submit" class="btn-success btn">Submit</button> </form> </div> </body> </html>
And I also did set the middleware for body-parser but It also don't work.而且我也确实为 body-parser 设置了中间件,但它也不起作用。 But It does work with GET requests.
但它确实适用于 GET 请求。 Why It doesn't work with the POST request.
为什么它不适用于 POST 请求。 If Anyone knows Please respond.
如果有人知道请回复。 It will be helpful.
这会很有帮助。
req.query
documentation : req.query
文档:
This property is an object containing a property for each query string parameter in the route.
此属性是一个 object,其中包含路由中每个查询字符串参数的属性。
What you're looking for is req.body
.您正在寻找的是
req.body
。
Also, since you seem to use file upload via multer
and may want to access that data, multer
documentation :此外,由于您似乎通过
multer
使用文件上传并且可能想要访问该数据, multer
文档:
Multer adds a body object and a file or files object to the request object .
Multer 将正文 object 和一个或多个文件 object 添加到请求 object中。 The body object contains the values of the text fields of the form, the file or files object contains the files uploaded via the form.
主体 object 包含表单文本字段的值,文件 object 包含通过表单上传的文件。
Since you're using multer.single
, you should have a req.file
(singular).由于您使用的是
multer.single
,因此您应该有一个req.file
(单数)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.