reactjs - 如何获取特定 API 列的值
问题描述
我正在创建一个电子商务网站,我正在尝试使用外键或我的表的第二列(即userID
使用 Axios GET 请求)获取某个购物车。这是我的表结构
cartID | userID | | productName
1 1 chair
2 3 ....
3 3 ....
如何显示唯一userID
的数据3
?
因为我不能这样做
axios.get("https://localhost:3000/api/carts/cartID/userID/3")
这就是我获取数据的方式
const [myCart, setMyCart] = useState([])
useEffect(() => {
axios
.get("https://localhost:3000/api/carts/")
.then((res) => {
setMyCart(res.data)
console.log(res.data)
})
}, []);
这就是我显示数据的方式
{myCart.map((item, index) => {
return(
<tr key={index.id}>
<td >{index + 1}</td>
<td>{item.name}</td>
</tr>
)}
)}
解决方案
您可以使用这样的条件:
{
myCart.map((item, index) => {
if (item.id === 3) {
return (
<tr key={index.id}>
<td>{index + 1}</td>
<td>{item.name}</td>
</tr>
);
}
return null;
});
}
推荐阅读
- javascript - 如何将类添加到 div 中的当前活动 div
- javascript - Ajax 轮询似乎只停留在最后一个请求时
- javascript - 嗨,我正在尝试在我的页面上复制具有不同元素的事件侦听器,将不同的元素与不同的键配对
- javascript - 如何在 Node JS 中修复 PayPal 的“验证错误”?
- javascript - 获取keyup后输入的值
- connection - 在 omnet++ 中,根据它们的欧几里德距离重复连接节点
- dart - 颤振输入意外结束(在字符1处)
- javascript - 闭环内循环 - FocusEvent 的问题
- reactjs - 由于在渲染方法中调用了 onClick 方法,出现“无法在现有状态转换期间更新”错误
- android - 如何使用 NavController.navigate 替换片段而不是将其添加到顶部