为什么这个获取请求不能按预期使用高级搜索?

编程入门 行业动态 更新时间:2024-10-06 01:39:32

为什么这个获取请求不能按预期使用<a href=https://www.elefans.com/category/jswz/34/1765723.html style=高级搜索?"/>

为什么这个获取请求不能按预期使用高级搜索?

当我运行请求/按下高级搜索的搜索按钮时,它会给我数据库中的所有动物,而不是高级搜索中指定的动物。正如在后端看到的,没有 console.log 在工作。

如何更改我的代码,以便在应用一个或多个过滤器时显示正确的动物

前端:

const getAdvancedPets = async (type, adoptionStatus, height, weight, name) =>{
        try{
            const res = await axios.get(`http://localhost:8080/api/all-pets?type=${type}&status=${adoptionStatus}&height=${height}&weight=${weight}&name=${name}`)
            // http://localhost:8080/api/all-pets/${type}/${adoptionStatus}/${height}/${weight}/${name}
            console.log(res)
            return res.data
        }catch(err){
            console.log(err)
        }
    }
    const animalTypeChange= (value) => {
        setAnimalType(value)
    }
    const adoptionStatusChange= (e) => {
        setAdoptionStatus(e.target.value)
    }
    const heightChange= (e) => {
        setHeight(e.target.value)
    }
    const weightChange= (e) => {
        setWeight(e.target.value)
    }
    const nameChange= (e) => {
        setName(e.target.value)
    }
    const handleAdvancedSearch = async () => {
        const type = animalType;
        const status = adoptionStatus;
        const petHeight = height;
        const petWeight = weight;
        const petName = name;
        const pets = await getAdvancedPets(type, status, petHeight, petWeight, petName);
        setPets(pets);
    }

后端:

petsRouter.get('/', async (request, response) => {
    // console.log(request.query); not loggin anything
    const { type, status, height, weight, name } = request.query;
    let typeQuery;
    if (type === 'other') {
      typeQuery = { $not: { $in: ['cat', 'dog'] } };
    } else {
      typeQuery = type;
    }
  
    const pets = await Pet.find({ type: typeQuery, status, height, weight, name });
    response.status(200).json(pets);
}); 
回答如下:

更多推荐

为什么这个获取请求不能按预期使用高级搜索?

本文发布于:2024-05-31 02:06:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1771162.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:高级搜索

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!