PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, October 12, 2022

[FIXED] How to send query parameters with Axios?

 October 12, 2022     axios, node.js, reactjs     No comments   

Issue

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);
    }
  );
};

Solution

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) {}
};


Answered By - Apostolos
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing