我正在使用 react、node、express、postgress。我將如何使用快速路由來傳遞多個引數?例如:
這是我的路線:
//Get entries from materials2 table
app.get("/materials2/:id1&:id2", async (req, res) => {
try {
const {material_thickness, material_width} = req.params;
const contact = await pool.query(
`SELECT *
FROM materials_inventory
WHERE
(material_thickness = $1) AND
(material_width BETWEEN ($2-0.5) AND ($2 0.5))`, [material_thickness, material_width]);
res.json(contact.rows[0]);
} catch (err) {
console.error(err.message);
}
})
這個想法是我需要將兩個引數傳遞給搜索查詢
從反應方面我有:
const stateSetter = async (thickness, width, length, length2) => {
try {
const response = await fetch(`http://localhost:5000/materials/${thickness, width}`, [thickness, width])
const jsonData = await response.json();
console.log(thickness);
} catch (err) {
console.log(err.message)
}
}
所以想法是 stateSetter 接收一些值,將這些值與路由一起使用。我該怎么辦?謝謝!
uj5u.com熱心網友回復:
您可以使用request query. 像這樣更改節點中的路線:
//Get entries from materials2 table
app.get("/materials2", async (req, res) => {
try {
const {materialThickness, materialWidth} = req.query;
const contact = await pool.query(
`SELECT *
FROM materials_inventory
WHERE
(material_thickness = $1) AND
(material_width BETWEEN ($2-0.5) AND ($2 0.5))`, [materialThickness, materialWidth]);
res.json(contact.rows[0]);
} catch (err) {
console.error(err.message);
}
})
你像這樣呼叫前端:
const stateSetter = async (thickness, width, length, length2) => {
try {
const response = await fetch(`http://localhost:5000/materials2?materialThickness=${thickness}&materialWidth=${width}`)
const jsonData = await response.json();
console.log(thickness);
} catch (err) {
console.log(err.message)
}
}
此外,您正在呼叫前端的端點materials表單,但將其指定為materials2在 node.js 中。我在答案中更新了這一點,但請記住。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/451893.html
標籤:javascript 节点.js 反应 表示 路线
下一篇:無法用Js停止音頻
