1

I have node api that returns data to browser using this api :

app.get('/api/patients',function(req,res){
    Patient.getPatients(function(err,patients){
        if(err){
            throw err;
        }
        console.log(patients.length);
        res.json(patients);

    });
});

I am trying to call this api from the service class.

import { Injectable } from '@angular/core';
import { Patient } from '../patient.interface';



@Injectable()
export class PatientDataService {

    patients : Patient[] =[];


    constructor() { }



    getAllPatients(): Patient[]
    {
          // What to do here ??
    }

}

How do i return data from node api to service ?

4
  • 1
    i think the above code is from angular2 Commented Mar 31, 2017 at 15:04
  • @PankajParkar This is an Angular 2 application, not an AngularJS (1) application. Commented Mar 31, 2017 at 15:06
  • $http and something related (but AngularJS 1) here Commented Mar 31, 2017 at 15:08
  • My bad.. didn't looked at Angular 2 tag.. though its would be same as http.get Commented Mar 31, 2017 at 15:15

2 Answers 2

2

Use this

@Injectable()
export class PatientDataService {

    patients : Patient[] =[];


    constructor(private http:Http) { }


        getAllPatients()
         {
          return this.http.get('base_url/api/people').map((res)=>res.json());

        }

}

and in your component inject this service and call

this.patientService.getAllPatients().subscribe((data)=>{
   //data is your patient list
})
Sign up to request clarification or add additional context in comments.

5 Comments

thank you .. this helped but now I am getting following error in browser console: XMLHttpRequest cannot load localhost:3000/api/patients. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'localhost:4200' is therefore not allowed access.
i fixed it by adding CORS in node api : app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
@DevHelp, you're making a call from an AngularJS application on one domain to your web server on another domain. Have you properly configured CORS on your web-server?
@DevHelp, never mind -- you beat me to it :) just be careful in any kind of production app using "*", this means any app on any domain can make a call to your web server.
Thanks Ben . How do I make sure to only allow some domains to access this service ?
2

You can first import the Angular2 Http library into your service:

import { Http } from '@angular/http';

I also import rx/js for use of Observables and mapping.

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

You can then inject the library into your service:

constructor(private _http: Http) { }

Make your http call to your node.js server like this:

getAllPatients(): Patient[]
{
      // What to do here ??
      return this._http.get('/api/patients')
           .map(this.extractData)
           .catch(this.handleError);
}

For more information, documentation, and clarification please read the Angular 2 Http Docs

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.