node.js - 如何将常量值与 api get 请求一起发送到 Node 服务器

标签 node.js angular api get

我有一个 API 服务,其中有一个方法可以通过 Node 服务器从 mongo 数据库获取数据。但我想发送 const userplant = localStorage.getItem("userplant"); 的值以及对我的 Node 服务器中的 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);
    });
  });

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

P.S.:我的 get 请求工作正常,我只想发送 const 值...

最佳答案

您可以使用 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);
    });
  });

关于node.js - 如何将常量值与 api get 请求一起发送到 Node 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58927362/

相关文章:

node.js - 如何使用模型模式创建环回远程方法?

java - 在本地创建 spring 和 nodejs 应用程序之间的 SSL 连接

angular2-mdl 组件应用于 Angular 2 Quickstart

css - Angular2 在运行时覆盖 css

php - 使用 cURL 通过 PHP 将文件发送到 API

javascript - 如何在发送初始 Web 服务器响应之前检测服务器端浏览器是否支持 Javascript?

node.js - PDF 文件在浏览器中下载不正确,但可以使用 wget 或使用 NodeJS/Express curl

angular - 在 jquery 函数中我可以调用 angular 5 函数吗

java - 使用基于 Grails JAX-RS 插件的 RESTful API 上传文件

ruby-on-rails - Rails Grape API 'id is invalid' 在不需要 id 的请求中