DEV Community

Nisal-Sashmitha
Nisal-Sashmitha

Posted on

How to make a get request using axios with params and read it in backend.

Let's see how to make a get request using axios with params and read the request and get params in backend.

FrontEnd

//take 'docID' as the id of the ducement you search
//take port number as 4200

axios
      .get("http://localhost:4200/api/stock/getProduct", {
        params: { id: docID },
      })
      .then(function (response) {
        console.log(response.data);
      })
      .catch(function (error) {
        console.log(error);
      })
      .then(function () {
        // run anything after the response is arrived
      });
Enter fullscreen mode Exit fullscreen mode

Backend

const router = express.Router();

router.get("/getCusProducts", getProduct);

function getProduct(req, res) {
  let id = req.query.id; //<<<this is how you read params

  Product.findById(id)    // Product is the model
    .then((product) => {
      res.json(product);
    })
    .catch((err) => {
      console.log(err);
    });
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)