如何从angular4组件传递参数来表达API

How to pass parameters from angular4 component to express API

我正在为平均堆栈应用程序工作。我可以连接 express api 和 angular 组件,但我想将参数传递给 api 服务。 请找到下面的代码以获得更清晰的想法,

组件代码

  constructor(private _dataService: DataService){
   var parametervalue = "Monthly";
   this._dataService.getexternalSourceDetailFiltered().subscribe((data) => {
   this.source.load(data);
    });}  

数据服务代码

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class DataService {
result;
constructor(private _http: Http) { }
getStudents(){
   return this._http.get('/external_sources').map(result =>
   this.result = result.json().data);
}
getexternalSourceDetail(){
   return this._http.get('/external_sources_details').map(result => 
   this.result = result.json().data);
}
getexternalSourceDetailFiltered(){
   return this._http.get('/external_sources_details').map(result => 
   this.result = result.json().data);
 }
}

快递API编码

router.get('/external_sources_details_filtered',(req,res) =>{
    connection((db) => {
        var intId = parseInt(0);
        var query ={'frequency.Monthly':{$exists:true}};
        var projection = {_id:0,sourceID:1,SourceName:1, Outstanding:1};    
        db.collection('external_sources').find(query).project(projection).
        toArray().then((external_sources_details_filtered) => {
        response.data = external_sources_details_filtered;
        res.json(response);
     })
    })
  })

我如何从组件传递 parametervalue 以便我可以在 express API 中使用它来将参数传递给使用动态参数

调用 mongodb

解决方案:作为全新的我四处搜索并找到了解决方案: 我使用 URLSearchParams 设置参数以通过 express API 传递。 这是更好理解的代码,

组件代码:

constructor(private _dataService: DataService){
    var param = new URLSearchParams();
    param.append('frequency','Monthly');
    this._dataService.getexternalSourceDetailFiltered(param).subscribe((data) => {
      this.source.load(data);

    });
   }  

数据服务代码

 getexternalSourceDetailFiltered(parameterValue:any ){
 return this._http.get('/external_sources_details_filtered', 
 { 
  params:parameterValue}).map(result => this.result = result.json().data);
 }

表达APIjs代码

  router.get('/external_sources_details_filtered',(req,res) =>{
    let parameterValue;
    connection((db) => {
        if(req.query.frequency != '')
        {
           parameterValue  = String( 'frequency.'+ req.query.frequency);
        }
       else
       {
             parameterValue = String( 'frequency');
       }
       console.log(parameterValue);
        var query = {[parameterValue] :{$exists:true}};
        var projection = {_id:0,sourceID:1,SourceName:1, Outstanding:1};
    db.collection('external_sources').find(query).project(projection).toArray().then((external_sources_details_filtered) => {
        response.data = external_sources_details_filtered;
        res.json(response);
    })
    })