Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何将参数从angular4组件传递到ExpressAPI_Node.js_Angular_Express_Mean Stack_Meanjs - Fatal编程技术网

Node.js 如何将参数从angular4组件传递到ExpressAPI

Node.js 如何将参数从angular4组件传递到ExpressAPI,node.js,angular,express,mean-stack,meanjs,Node.js,Angular,Express,Mean Stack,Meanjs,我为平均堆栈应用程序工作。我能够连接ExpressAPI和angular组件,但我想将参数传递给api服务。 请查找下面的代码,以获得更清晰的想法 组件代码 constructor(private _dataService: DataService){ var parametervalue = "Monthly"; this._dataService.getexternalSourceDetailFiltered().subscribe((data) => { thi

我为平均堆栈应用程序工作。我能够连接ExpressAPI和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);
 }
}
 getexternalSourceDetailFiltered(parameterValue:any ){
 return this._http.get('/external_sources_details_filtered', 
 { 
  params:parameterValue}).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);
     })
    })
  })

如何从组件传递参数值,以便在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);

    });
   }  
数据服务代码

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);
 }
}
 getexternalSourceDetailFiltered(parameterValue:any ){
 return this._http.get('/external_sources_details_filtered', 
 { 
  params:parameterValue}).map(result => this.result = result.json().data);
 }
expressapi-js代码

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

参数值将是静态的还是动态的?它将是动态的@yer