How to pass parameters from angular4 component to express API

76 views Asked by At

I am working for mean stack application. I am able to connect express api and angular component, but i want to pass parameters to the api service. Please find the code below for clearer idea,

Component Code

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

DataService Code

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

Express API Code

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

How would i pass parametervalue from the component so that i can use it in express API to pass parameter to call mongodb using dynamic parameter

1

There are 1 answers

0
Shashank S Chandel On BEST ANSWER

SOLUTION: Being totally new i searched around and found a solution: i used URLSearchParams to set the parameter to pass through the express API. Here is the the code for better understanding,

Component Code:

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

    });
   }  

Data Service Code

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

Express API js Code

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