Angular does not include a particular parameter in the HTTP request

I am currently facing an unusual issue in Angular and Ionic 2 related to a specific provider I have set up. Here is the code snippet:

  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())

  };

When the category is set to all, everything works as expected. However, if the category is any other value, upon inspecting the Network tab in Chrome's Dev tools, I notice that the called URL does not include the search parameter. It only displays _labels and paging. Surprisingly, the console.log statement inside the conditional block prints out the correct URL. Testing that URL in Postman retrieves the filtered results successfully.

Despite trying various methods of concatenating strings, I consistently face this issue.

Answer №1

Encountering a scope issue due to the block-scoped nature of let.

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

This variable url is scoped within an if (...) { ... } block, creating two distinct variables with the same name. The assigned value inside the if block is never utilized (aside from console.log).

The corrected implementation should be:

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

A guide on crafting a type definition for the action parameter in the React useReducer hook with Typescript

In this scenario, let's consider the definition of userReducer as follows: function userReducer(state: string, action: UserAction): string { switch (action.type) { case "LOGIN": return action.username; case "LOGOUT": return ""; ...

After updating Jest, encountering an error when trying to access the 'html' property of an undefined value

In upgrading jest to version 28 for my angular v12 project, I encountered an error after the update: FAIL src/app/components/update-input/update-input.directive.spec.ts ● Test suite failed to run TypeError: Cannot read property 'html' of un ...

Linking Two HTML Components in Angular 4 with Identical Values

Recently, I've started working with Angular and encountered an issue. In a table row, the value item.md_id is bound like this: <tr *ngFor="let item of driverData"> <td class="align-right" id="md_id" [(ngModel)]="item.md_id" name="driverId ...

Setting up a Docker configuration for an Angular project

Apologies for my imperfect English. I am new to Docker and finding it challenging. Currently, I am trying to configure this project: After adding two files https://i.sstatic.net/NyEcN.png This is my docker-compose file web: build: . ports: ...

Attaching an object to an input field in Ionic 2

I've come across a fundamental question that I can't seem to find the answer for online. Within my Ionic 2 application, I have an AddPassengerPage containing input fields for name, email, and phone. In my module, I define these variables: name: ...

Transmit JWT through Ionic3 using the Ionic Super Starter template

Currently, I am storing a token in local storage and using the ionic-starter-super framework which includes a "generic REST Api handler". However, I have encountered an issue when trying to add a JWT token and send it along with other data. Below is an ex ...

Tips for preventing duplicate Java Script code within if statements

In my function, there are various statements to check the visibility of fields: isFieldVisible(node: any, field: DocumentField): boolean { if (field.tag === 'ADDR_KOMU') { let field = this.dfs_look(node.children, 'ADDR_A ...

Integrating Laravel 5.6 with Angular 6 for seamless functionality

For my project, I am considering using Laravel as the backend and Angular as the frontend. There are two methods that I can use to integrate these frameworks: 1) I could integrate both frameworks by utilizing an API service, or 2) I could opt for a monol ...

The user authentication service indicates that no user is currently logged in

Currently, I am working on implementing a route guard to distinguish between authenticated users and guests. In order to achieve this, I have created an auth-guard service along with an auth service. Although the user data is successfully stored in the loc ...

Issue with Angular DevExtreme error popup

Currently, I am working on a project using Angular and DevExtreme. Whenever an error occurs, the error message seems to be hidden behind the popup form. Can anyone advise me on how to bring it to the front? Any help would be greatly appreciated. https://i ...

There is no valid injection token found for the parameter 'functions' in the class 'TodosComponent'

While working in my code, I decided to use 'firebase' instead of '@angular/fire'. However, I encountered an issue that displayed the following error message: No suitable injection token for parameter 'functions' of class &apos ...

The app engine deployment is not implementing gzip compression on my content

Update Status An update is expected to be rolled out by the end of this month. I recently deployed a few websites on Google App Engine using standard environment NodeJS. It's been a bit delayed, but I'm hoping it's just something I overloo ...

Tips for controlling the upload of a .exe.png file or converting a .exe file to a png file for uploading in angular 8

I had originally set up restrictions to only allow image file types such as JPG, JPEG, PNG, and TIFF. However, I discovered that users were able to upload .exe files simply by renaming them. For example, changing dell.exe.png or dell.exe to dell.png allo ...

Experiencing issues with Errors when Targeting ES5 in Angular2 TypeScript?

In my development environment, the npm version is 3.10.10, and I want to create a new Angular2 project from scratch. When I try running npm install angular2 --save I encounter this error message: Error Image After referring to this answer which recomm ...

Issue arises when ngModelGroup exists in child component without a provider for ControlContainer

I'm struggling to solve this issue, it's just not functioning as expected. Here is the original plunker created by Pascal Prekht, providing an explanation on template driven forms: And my version can be found here. It mirrors the original but w ...

Troubleshooting a custom pipe issue in Ionic v4 with Angular

I attempted to create a pipe in the ionic -v4 beta version to reverse an array, but encountered a parser error in the template. Below is the example of what I tried: ionic g pipe pipe/reverse Here is the pipe definition: import { Pipe, PipeTransform } f ...

Refactoring TypeScript components in Angular

How can I streamline the TypeScript in this component to avoid repeating code for each coverage line? This angular component utilizes an ngFor in the HTML template, displaying a different "GroupsView" based on the context. <div *ngFor="let benefitG ...

Tips for implementing a guard feature using a modal window

I am looking to implement a dialog window with yes and no responses when clicking on a router link. If the user selects 'yes', I want to pass through the canActivate guard. The issue arises when returning to the same router with the guard in pla ...

Angular not displaying retrieved data

Having an issue with fetching data from an API using Angular. Although the console log confirms that the data is successfully fetched, it doesn't display on the page. Any assistance would be greatly appreciated. Take a look at my code: app.component. ...

Steps to extract key and value from a collection in Firebase database

In my Firebase database, I have a specific structure that I need to retrieve and display using ngFor in my view. However, I also need access to the unique key generated by Firebase when using the push method for each object. https://i.sstatic.net/nR2nK.pn ...