0

I have a service in which I have login function like this

public userLogin(body) {
 return new Promise((resolve, reject) => {
  this.apiService
    .generateApiRequest({
      route: Constants.API_ROUTES.USER_LOGIN.route,
      type: Constants.API_ROUTES.USER_LOGIN.type,
      body: body
    })
    .map(res => res.json())
    .subscribe(response => {
      resolve(response);
     });
   });
 }

and have generateApiRequest function in utilities like this

generateApiRequest(request, isTokened = false) {
    var res;
    let response;
    if (!request.headers) {
        request.headers = new HttpHeaders();
    }
    if (isTokened) {
        request.headers.append('Token', this.localCache.getToken());
    }
    switch (request.type) {
        case Constants.REQUEST_TYPE.GET:
            response = this.get(request);
            break;
        case Constants.REQUEST_TYPE.POST:
            response = this.post(request);
            break;
        case Constants.REQUEST_TYPE.PUT:
            response = this.put(request);
            break;
        case Constants.REQUEST_TYPE.DELETE:
            response = this.delete(request);
            break;
    }
    return response;
}

When I upgraded older http to new HttpClient so after this I have to change map inside pipe function so I did like this

public userLogin(body) {
return new Promise((resolve, reject) => {
  this.apiService
    .generateApiRequest({
      route: Constants.API_ROUTES.USER_LOGIN.route,
      type: Constants.API_ROUTES.USER_LOGIN.type,
      body: body
    })
    .pipe(map(res => res.json()))
    .subscribe(response => {
      resolve(response);
     });
  });
 }

But compiler is giving an error at this line .pipe(map(res => res.json()))

How can I fix it?

2
  • Please refer to the documentation: angular.io/guide/http Commented Sep 13, 2019 at 9:31
  • 1
    Now you already got an answer, but for the future, please post the error you get as well. "compiler is giving an error" isn't always enough information Commented Sep 13, 2019 at 9:36

2 Answers 2

2

Right, that's because new http client by default calls res.json() implicitly and you don't need to that manually yourself. Just remove .json() call, so you don't need to use 'map' at all in your case.

Sign up to request clarification or add additional context in comments.

6 Comments

If you are going to copy paste part of someone elses answer, please give the credits... stackoverflow.com/a/45780553/6294072
So should i replace this .pipe(map(res => res.json())) to .pipe() ?
It should work, but you can also remove the whole line
You don't need to use .pipe() at all, just leave only .subscribe with your code.
you mean this .generateApiRequest({ route: Constants.API_ROUTES.USER_LOGIN.route, type: Constants.API_ROUTES.USER_LOGIN.type, body: body }) .subscribe(response => { resolve(response); }); Instead of .generateApiRequest({ route: Constants.API_ROUTES.USER_LOGIN.route, type: Constants.API_ROUTES.USER_LOGIN.type, body: body }) .pipe(map(res => res.json())) .subscribe(response => { resolve(response); });
|
0

Since Angular Http client already map response from API by default is JSON.

You can simply remove the map

public userLogin(body) {
 return new Promise((resolve, reject) => {
  this.apiService
    .generateApiRequest({
      route: Constants.API_ROUTES.USER_LOGIN.route,
      type: Constants.API_ROUTES.USER_LOGIN.type,
      body: body
    })
    .subscribe(response => {
      resolve(response);
     });
  });
 }

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.