【发布时间】:2021-05-08 22:19:10
【问题描述】:
当我使用 post 请求时,它只发布 null 我使用 postman
app.post("/books",async (req,res)=>{
try{
const {book_name,book_author,book_desc} = req.body;
const newBook = await pool.query("INSERT INTO library(book_name,book_author,book_desc) VALUES($1,$2,$3) RETURNING *",
[book_name,book_author,book_desc]);
res.json(newBook.rows[0]);
}
catch(err){
console.error(err.message);
}
})
这是邮递员的代码和输出:
{ “书名”:“abc”, "book_author": "def", "book_desc": "gjl"
}
输出:
{ “book_id”:1, “书名”:空, “book_author”:空, “book_desc”:空 }
【问题讨论】:
-
当你在res.json之前做console.log(newBook.rows[0]),会打印出什么?
-
还是空
-
可能是因为插入没有返回任何内容?表示没有插入任何行?
-
输出是它返回的问题,我认为问题可能是给 req.body 多个值,但我仍然找不到解决方案