我已经使用React应用程序设置了JSON服务器,我对终点有一个快速的问题
这是我的一些数据:
{
"products": [
{
"name": "football",
"id": "SPO-001",
"category": "sport",
"price": "40",
"coupons": ["daw124qdw", "a1212cxn"]
},
{
"name": "cricket bat",
"id": "SPO-002",
"category": "sport",
"price": "80"
}
]
}
,当我做这样的事情时,一切都很好:
axios.get('http://localhost:3004/products').then(({ data }) => {
// do something with data
})
我知道我可以做: http://localhost:3004/products/SPO-001
,它返回了我的ID。但是,我该如何返回该特定ID的优惠券条目?这是可能的端点吗?
我尝试了axios.get('http://localhost:3004/products/SPO-001/coupons')
,但我刚刚有一个空的对象。有什么想法吗?
您有2个选项:
1st
axios.get('http://localhost:3004/products')
.then(res => this.setState({product: res.data.productID.coupon})) // not sure how you want to use your response, this is just an example
第二个为每种产品创建您的终点,例如http://localhost:3004/:productID/coupon
,然后"命中"该端点,然后使用您的Axios获取请求,例如axios.get('http://localhost:3004/23/coupon').then(res => res.data)
我不得不说第二个选项会更强大。