Tasos
Tasos

Reputation: 7577

A specific parameter is not included in HTTP request on Angular

I have a strange issue in Angular and Ionic 2. I have the following provider:

  getTransportations = function (category, maxResults) {    
    let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}`;
    if (category !== 'all') {
      let search = {
        field_filters: [{
          key: '2',
          operator: 'is',
          value: category
        }]
      };
      let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`;
      console.log(url);

    }
    let headers: Headers = new Headers();
    headers.append("Authorization", "Basic " + this.bt);
    headers.append("Content-type", "application/json");
    return this.http.get(url, {
        headers: headers
      })
      .map(response => response.json())

  };

If category is all then the provider works as expected. However, if the category is something else, on the Dev tools on Chrome, in the tab of Network, I can see the called url without the search parameter. Just _labels and paging. But, the console.log I added inside the if works and it prints the right url that if I use it on Postman, I get the filtered results I want.

I tried different ways to concat strings, but I have the same problem all the time.

Upvotes: 3

Views: 76

Answers (1)

Estus Flask
Estus Flask

Reputation: 222498

There is scope problem. let is block scoped.

let url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`;

has a scope that belongs to if (...) { ... }. These two url are different variables, and the one that is being assigned inside if is never used (except console.log).

It should be

url = `${this.domain}/wp-json/gf/v2/forms/1/entries?_labels=1&paging[page_size]=${maxResults}&search=${encodeURI(JSON.stringify(search))}`;

Upvotes: 1

Related Questions