简体   繁体   中英

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. I also tried with body-parser but it also didn't worked. 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. But It does work with GET requests. Why It doesn't work with the POST request. If Anyone knows Please respond. It will be helpful.

Thank You Very Much

req.query documentation :

This property is an object containing a property for each query string parameter in the route.

What you're looking for is req.body .

Also, since you seem to use file upload via multer and may want to access that data, multer documentation :

Multer adds a body object and a file or files object to the request 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.

Since you're using multer.single , you should have a req.file (singular).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM