Skip to content Skip to sidebar Skip to footer

Send Data From Node.js To Angular With Get()

I want to retrieve data (an array) from a node.js file with angular. I tried on my node.js file: app.get('/search', function(req, res){ res.send(data); }); And I've some diffi

Solution 1:

You need to subscribe to the observable

     let observable: Observable<Response> = this.http.get('/search',requestOptions);
     observable.subscribe((response: Response) => {
     let responseData: any = response.arrayBuffer().byteLength > 0 ? response.json() : {}; 
 });

Solution 2:

Try this.

data: any;

getDictionnary(): Observable<String[]> {
    return this.http.get('/search').pipe(map(data => this.data=data.json()));
};

ngOnInit() {
    this.getDictionnary().subscribe((response) => {
       this.data = response;
    });
}

Solution 3:

You can try this solution as given :

In HttpService:

  import { Injectable } from '@angular/core';

    import {
        HttpClient,
        HttpErrorResponse,
        HttpHeaders
    } from '@angular/common/http';

    import { Observable ,  Subject } from 'rxjs';

    @Injectable({
        providedIn: 'root'
    })
    export class HttpService {

        constructor(private httpClient: HttpClient) {


        }

      getData(url,body, options): Observable<any>{
          return this.httpClient.get(url, options);
        }
    }

In component

private data: string[] = [];
constructor(private http: HttpService){
this.getDictionnary();
}

getDictionnary(){

this.http.getData(url,options).subscribe((resp:any) => {

 this.data = resp.data

}
}

Post a Comment for "Send Data From Node.js To Angular With Get()"