Home > Mobile >  How to send query parameters with Axios?
How to send query parameters with Axios?

Time:09-23

My question is similar to this How to post query parameters with Axios? Instead of posting, I want a get data request and I wanna pass the query param name to the request. It works in postman but not working in react.

const handleSubmit = async () => {
try {
  const res = await axios.get(
    "http://localhost:5000/api/products",
    {},
    {
      params: {
        name,
      },
    }
  );
  console.log(res.data);
} catch (err) {}
};





 exports.retrieveProducts = (req, res) => {
   Product.find(
    { name: { $regex: req.query.name, $options: "i" } },
    (err, products) => {
      if (err) res.status(500).json(err);
      res.json(products);
    }
  );
};

CodePudding user response:

You are using an empty object as config.

It should be

const handleSubmit = async () => {
try {
  const res = await axios.get(
    "http://localhost:5000/api/products",
    {
      params: {
        name: 'whatever',
      },
    }
  );
  console.log(res.data);
} catch (err) {}
};
  • Related