[Fixed] how to send data from node.js to angular?

Issue

server ( node js )

app.get('/api/GetFlux',function(req,resp){
  let data = "hello"
  resp.send(JSON.stringify(data));
})

Client (Angular)

GetFlux():Observable<any[]>{
  return this.http.get<any>(this.APIUrlNode+'/GetFlux');
}


this.service.GetFlux().subscribe(data =>
      {
        this.fluxList = data;
      });

fluxList undefined why ??

Solution

The problem is you are sending a string back. If you really want to send a string back you need to amend your service with:

return this.http.get<any>(this.APIUrlNode+'/GetFlux', { responseType: 'text'});

JSON is the default responseType. What your api is sending and your service need to match. If you really meant to send json which is what you would normally do, you need to amend your api to:

resp.json(data);

Really depends on the nature of the REAL data you are sending.

Leave a Reply

(*) Required, Your email will not be published