最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

网站源码admin34浏览0评论

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

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

当我运行请求/按下高级搜索的搜索按钮时,它会给我数据库中的所有动物,而不是高级搜索中指定的动物。正如在后端看到的,没有 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);
}); 
回答如下:
发布评论

评论列表(0)

  1. 暂无评论