我有一个 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/