我將資料發布到 mongodb 如下:
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:true}));
app.use('/insert',async(req,res)=>{
const newProduct=new product({
_id:req.body._id,
name:req.body.name,
description:req.body.description
});
try{
await newProduct.save();
// res.json(newProduct);
res.send(`${newProduct} inserted.`);
//res.redirect('/');
}
catch(err){
res.send(err);
}
});
資料取自用戶。用戶通過 URL 向 mango 發送資料。用戶在 URL 中輸入資料如下:
http://localhost:3000/insert?_id=23&name=GLX 2690&description=classic phone
但收到一個空回應并且資料不會插入到 mongo。如何通過 URL 發送資料?上面的網址錯了嗎?
uj5u.com熱心網友回復:
我認為是req.query,不是req.body。
uj5u.com熱心網友回復:
1-嘗試將 app.use 更改為 app.post
2-嘗試使用這個網址:http://localhost:3000/insert?_id=23&name=GLX+2690&description=classic+phone
3- 使用 req.query
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/446750.html
上一篇:React-Node應用程式中的POST和PATCH表單操作后瀏覽器URL發生更改
下一篇:我被困在“需要運送物品的跟蹤號”
