【问题标题】:How to send constant value along with api get request to node server如何将常量值与 api get 请求一起发送到节点服务器
【发布时间】:2020-03-14 13:52:34
【问题描述】:

我有一个 api 服务,其中有一种方法可以通过节点服务器从 mongo db 获取数据。但我想将 const userplant = localStorage.getItem("userplant"); 的值连同 get 请求一起发送到我的节点服务器中的 GET 路由器,这样我就可以使用 WHERE 条件过滤数据。

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

我想要{plantId : "here"} 旁边的userplant 的值

P.S.:我的 get 请求运行良好,我只想将 const 值连同它一起发送...

【问题讨论】:

    标签: node.js angular api get


    【解决方案1】:

    在 Angular 中进行如下更改,可以像这样传递标头和参数:

    const httpOptions = {
            headers: { 'Content-Type': 'application/json' },
            params: {userplant:userplant}
        };
    getStorageLocationsList(){
        this.setHeader();
        const userplant = localStorage.getItem("userplant"); 
        return this.http.get(this.localURL + 'storagelocation/view',httpOptions);
      }
    

    在Node Js中做如下改动,你必须使用body parser。

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

    【讨论】:

      【解决方案2】:

      您可以使用 queryParams 来做到这一点:

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

      【讨论】:

        【解决方案3】:

        您可以在查询参数中发送。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-10-19
          • 2021-03-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多