簡體   English   中英

在發布請求時,req.query 在 express 節點中返回空字典

[英]On post request req.query returns empty dictionary in express node

我想獲取后查詢值以在數據庫中創建一個新行。 但是在 req.query 中它不起作用。 我也嘗試過使用 body-parser,但它也沒有奏效。 編碼

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')
})

和客戶端代碼

 <,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>

而且我也確實為 body-parser 設置了中間件,但它也不起作用。 但它確實適用於 GET 請求。 為什么它不適用於 POST 請求。 如果有人知道請回復。 這會很有幫助。

非常感謝您

req.query文檔

此屬性是一個 object,其中包含路由中每個查詢字符串參數的屬性。

您正在尋找的是req.body

此外,由於您似乎通過multer使用文件上傳並且可能想要訪問該數據, multer文檔

Multer 將正文 object 和一個或多個文件 object 添加到請求 object中。 主體 object 包含表單文本字段的值,文件 object 包含通過表單上傳的文件。

由於您使用的是multer.single ,因此您應該有一個req.file (單數)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM