Node.js 如何将常量值与api get请求一起发送到节点服务器
我有一个api服务,其中有一个通过节点服务器从mongo db获取数据的方法。但是我想发送Node.js 如何将常量值与api get请求一起发送到节点服务器,node.js,angular,api,get,Node.js,Angular,Api,Get,我有一个api服务,其中有一个通过节点服务器从mongo db获取数据的方法。但是我想发送constuserplant=localStorage.getItem(“userplant”)的值以及对我的节点服务器中的get路由器的get请求,以便我可以使用WHERE条件过滤数据 API.SERVICE.TS getStorageLocationsList(){ this.setHeader(); const userplant = localStorage.getItem("use
constuserplant=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.get('/storagelocation/view', auth, function(req, res, next) {
StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
if (err) return next(err);
res.json(events);
});
});
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.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);
});
});
ROUTER.JS
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.get('/storagelocation/view', auth, function(req, res, next) {
StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
if (err) return next(err);
res.json(events);
});
});
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.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);
});
});
我想在{plantId:“here”}
注意:我的get请求工作正常,我只想将常量值与之一起发送…在您可以发送的查询参数中
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});
}
您可以使用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', {headers:this.appHeader});
}
router.get('/storagelocation/view', auth, function(req, res, next) {
StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
if (err) return next(err);
res.json(events);
});
});
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.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);
});
});
ROUTER.JS
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.get('/storagelocation/view', auth, function(req, res, next) {
StorageLocation.find({plantId : "5dd262a61120910d94326cc1"}, function (err, events) {
if (err) return next(err);
res.json(events);
});
});
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.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);
});
});
在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);
}
在NodeJS中进行以下更改,您必须使用body解析器
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);
});
});