Search code examples
node.jsangularapiget

How to send constant value along with api get request to node server


I have a api service in which there is a method to fetch data from the mongo db through node server. But i want to send the value of const userplant = localStorage.getItem("userplant"); along with the get request to the GET router in my node server so i can filter the data with a WHERE condition.

API.SERVICE.TS

getStorageLocationsList(){
    this.setHeader();
    const userplant = localStorage.getItem("userplant"); //I Want to send this to the GET router
    return this.http.get(this.localURL + 'storagelocation/view', {headers:this.appHeader});
  }

ROUTER.JS

router.get('/storagelocation/view', auth, function(req, res, next) {
    StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
      if (err) return next(err);
      res.json(events);
    });
  });

I want the value of userplant next to the {plantId : "here"}

P.S.: My get request is perfectly working , i just want to send the const value along with it...


Solution

  • You can use queryParams to do that:

    API.SERVICE.TS

    getStorageLocationsList(){
        this.setHeader();
        const userplant = localStorage.getItem("userplant"); //I Want to send this to the GET router
        return this.http.get(this.localURL + 'storagelocation/view' + '?userplant=' + userplant , {headers:this.appHeader});
      }
    

    ROUTER.JS

    router.get('/storagelocation/view', auth, function(req, res, next) {
        let userplant = req.query.userplant;
        // you can use it now
        StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
          if (err) return next(err);
          res.json(events);
        });
      });