1

Unable to resolve function map(). I also tried importing rxjs/add/operator/map

getVenues() {
if (this.placeValue != null && this.placeValue != "" && this.recipeValue != null && this.recipeValue != "") {
  this._http.get("https://api.foursquare.com/v2/venues/search" +
    "?client_id=3PPNMTIKJJNDVYPFOBGSHHV2PR5A2P05PYHXDN2MKSKTTBSX" +
    "&client_secret=0QPHT0F5RS043F4TB4KKPQSHKSAXKE5ZNOYGB5KL2MBDYAG4" +
    "&v=20160215&limit=5" +
    "&near=" + this.placeValue +
    "&query=" + this.recipeValue)
    .map(res => res.json())
    .subscribe(data => {
      // we've got back the raw data, now generate the core schedule data
      // and save the data for later reference
      this.data1 = data.results[0].key;
      console.log(this.data1)},
      err => console.log(err)

      );

If I use HttpClientModule instead of HttpClient, I get the error

Unable to resolve method get()

2
  • Can you provide stackBlitz for this? or you can convert that in to promise also Commented Jan 19, 2019 at 5:42
  • Which version of Angular do you use? If its greater than 6 then wrap your map with a pipe method, or you don't have to manually convert to json as by default you will get parsed json only. Commented Jan 19, 2019 at 6:31

1 Answer 1

1

Normally we use pipe() when using httpclient.

this.http.get(url).pipe(map(a => b), catchError(error => this.handleError(error)));

Just as its comments of httpClient.get():

/**
 * Construct a GET request which interprets the body as JSON and returns it.
 *
 * @return an `Observable` of the body as type `T`.
 */

Since it's returning an Obervable then you follow the pipe to merge the functions into a chain.

Tips

  • you should use !== and === in JS to avoid weird results;
  • normally we do not subscribe directly after the get and we put the get method in a separate service;
Sign up to request clarification or add additional context in comments.

2 Comments

Got to know that HttpClient.get() applies res.json() automatically and returns Observable<HttpResponse<string>>. So, the map() can be removed. As said, if we donot subscribe() directly after get, when to do it?
Move the get methods to a service and subscribe when used in the components, it’s easier to test and manage since the components shall not consider how to get the data actually.

Your Answer

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