如何用axios发送查询参数?

Und*_*sed 1 node.js reactjs axios

我的问题类似于How to post query parameters with Axios? 我不需要发布,而是想要一个获取数据请求,并且我想将查询参数名称传递给请求。它在邮递员中有效,但在反应中无效。

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);
    }
  );
};
Run Code Online (Sandbox Code Playgroud)

Apo*_*los 6

您正在使用一个空对象作为配置。

它应该是

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