【问题标题】:How to get values on specific API column如何获取特定 API 列的值
【发布时间】:2021-08-24 02:01:07
【问题描述】:

我正在创建一个电子商务网站,我正在尝试使用 Axios GET 请求获取具有外键或我的表的第二列 userID 的某个购物车。 这是我的表结构

cartID | userID | | productName
1          1            chair
2          3            ....
3          3            ....

如何显示3 中仅有userID 的数据?

因为我不能这样做 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>
  )}
)}

【问题讨论】:

    标签: reactjs api axios


    【解决方案1】:

    你可以使用这样的条件:

    {
      myCart.map((item, index) => {
        if (item.id === 3) {
          return (
            <tr key={index.id}>
              <td>{index + 1}</td>
              <td>{item.name}</td>
            </tr>
          );
        }
    
        return null;
      });
    }
    

    【讨论】:

    • 哇,就是这么简单!谢谢。顺便说一句,我将if (index.id === 3) { 更改为if (userID.id === 3) {
    • 是的。只需从myCart 更新item.id
    • 我的意思是 if (index.id === 3) if (item.userID === 3) { ,抱歉,我无法编辑上面的评论。
    猜你喜欢
    • 2021-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    相关资源
    最近更新 更多