The filters query is not working

System Information
  • Strapi Version: 4.10.5
  • Operating System: Windows 10
  • Database: SQLite
  • Node Version: v18.14.2
  • NPM Version: 9.5.0
  • Yarn Version:

Hi everyone.
I’m fetching data from Strapi which works fine, except when I try to filter the products using the [filters] query. It returns an empty array instead of the products array, but all the products reappear when I remove the [filters] query. I’m trying to get products filtered by [type]. Can anyone help me?

Here’s my code:

import Card from "../Card/Card";
import { useState, useEffect } from "react";
import "./Featured.scss";
import axios from "axios";

const Featured = ({ type }) => {

  const [data, setData] = useState([]);

  useEffect(() => {
    const fetchData = async () => {
      try{
        const res = await axios.get(
          import.meta.env.VITE_APP_API_URL + `/products?populate=*&[filters][type][$eq]=${type}`,
          {
            headers: {
              Authorization: "bearer" + import.meta.env.REACT_APP_API_TOKEN,
            },
          }
        );
        setData(res.data.data)
      }catch(err){
        console.log(err)
      }
    };
    fetchData();
  }, [type]);

  console.log(data);

  return (
    <div className="featured">
      <div className="top">
        <h1>{type} products</h1>
        <p>
          Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do
          eiusmod tempor incididunt ut labore et dolore magna aliqua. Quis ipsum
          suspendisse ultrices gravida. Risus commodo viverra maecenas accumsan
          lacus vel facilisis labore et dolore magna aliqua. Quis ipsum
          suspendisse ultrices gravida. Risus commodo viverra maecenas.
        </p>
      </div>
      <div className="bottom">
        {data.map(item => (
          <Card item={item} key={item.id} />
        ))}
      </div>
    </div>
  )
};

export default Featured;

Maybe you have already solved it but I leave my solution in case someone else finds it useful:
I had that problem and in my case it was because I could not filter a field that has the attribute “private”.
my query looks like this: api/products?filters[category][id][$eq]=${myCategoryId}

you can see it in the schema.json: api/product/content-types/product/schema.json
imagen

or you can also check in admin panel, in your case products > types > advanced settings and check that the private field checkbox is unchecked.