我已通过此代码将类别ID发送给Nodejs
const catHandler = async (catId) => {
const data = Axios.put('/api/products/categories/filter', {catId: catId},
{
"headers": { "content-type": "application/json", },
}
).then( categoriesProducts => {
console.log(categoriesProducts.data.products)
})
}
这是我的路线
router.put('/categories/filter', async (req, res) => {
try {
const findCategory = await Category.find({ _id: req.body.catId });
if (findCategory) {
const productsByCategory = await Product.find(
{ category: req.body.catId }
).then(products => {
res.status(200).json({ products });
})
}
} catch (error) {
console.log('categories filter error', error)
}
})
特定类别的产品显示在反应前端侧的console.log(categoriesProducts.data.products)
中,如下所示
0: {_id: "5f7c88756746363148792982", name: "Simple Pizza", price: 5.6, image: "1601996916374.jpg", countInStock: 434, …}
1: {_id: "5f7c88976746363148792983", name: "Smoked Pizza", price: 7.8, image: "1601996951114.jpg", countInStock: 88, …}
2: {_id: "5f7c88c56746363148792984", name: "Large Cheezy Pizza", price: 9.4, image: "1601996997474.jpg", countInStock: 434, …}
但是我想在前端显示这些产品。我尝试使用axios.get方法,但是使用get方法时,如何将类别ID发送到后端。所以,如果有人知道如何做,请Plz指导我。
答案 0 :(得分:0)
您可以在节点js中将查询参数与get方法一起使用
您可以在nodeJs中通过req.query
获取查询参数
示例 从前端传递类别ID-
api/products/categories/filter?cid=1
在后端获取查询参数
const catId = req.query.cid
答案 1 :(得分:0)
您可以使用以下代码将参数传递给API get方法。
async fetch ({ error, store }) {
try {
await store.dispatch( 'article/fetchOne' , { articleSlug: this.articleSlug })
} catch (err) {
error({ statusCode: 404, message: 'May the force be with you' })
}
此外,您还首先创建一个名为Items的新状态,如下所示。
sudo yum list --installed | more
最后遍历项目。
BR