What could be causing my function to return as undefined the second time it's invoked?

Within my approach

private onDataLoadSuccessful(users: User[], roles: Role[]) {

    this.alertService.stopLoadingMessage();
    this.loadingIndicator = false;
    this.dataSource.data = users.map(data => {
        let newData: User;
        newData = Utilities.toCamel((data));
        return newData;
    });

    this.allRoles = roles.map(function (data) {
        let newData: Role;
        newData = Utilities.toCamel(data);
        return newData;
    });
}

I am utilizing the Utilities.toCamel(data) function twice. Firstly, to convert the Users[] array to camel case and secondly, on the Roles[] array

The issue arises when it is called a second time:

 this.allRoles = roles.map(function (data) {
        let newData: Role;
        newData = Utilities.toCamel(data);
        return newData;
    });

This leads to the error message:

TypeError: Cannot read property 'toCamel' of undefined

Is there something that I am overlooking? Thanks!

Answer №1

Approach it as if it's your initial attempt:

For each role in the roles array, we are assigning it to a new variable newData after converting it to camel case using Utilities.toCamel function:
this.allRoles = roles.map(data => {
    let newData: Role;
    newData = Utilities.toCamel(data);
    return newData;
});

Answer №2

When working with roles, it is important to utilize arrow functions, denoted by () =>, rather than the traditional function keyword. Take a look at this example:

this.roleList = list.map((item) => {
    let updatedItem: Role;
    updatedItem = DataProcessor.convertToCamel(item);
    return updatedItem;
});

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

How to Apply a CSS Class to the Body Tag in Angular 2.x

How can I add [class.fixed]="isFixed" to the body tag when Angular 2.x is bootstrapped inside the body (outside my-app)? <html> <head> </head> <body [class.fixed]="isFixed"> <my-app>Loading...</my-app> </body> & ...

Guide on displaying tooltip messages for a custom directive in Visual Studio Code

I have developed a custom directive called app-subscriber. When I hover the mouse over it, I want to display a tooltip message saying "This is for subscribers and requires an email address". Is this achievable? Do I need to create a new VS Code extension f ...

Passing additional parameters to an Angular directive individually

Is there a way to pass two parameters separately to my directive instead of as one combined parameter? Currently, I am able to add the parameters as one parameter (*ovLoading="!isDataReceived;noBackground:true"), but I would prefer to have them as two sepa ...

Is there a way to display my modal separately from my sidenav while utilizing :host in Angular?

I implemented a :host with hostlistener() in my navmenu-component.ts to enable a sidemenu that slides out from my sidenavbar when a button is pressed. My goal is to display a modal for editing purposes. I have included the modal in the navmenu-component.h ...

Ways to make a component gradually appear and disappear in an Angular application

I have developed a task management application using Angular and I wanted to implement a fading effect for each task when it is created and before it is deleted. Despite successfully applying the fade in effect at the todo-item component level, I encounter ...

Having trouble retrieving values from radio buttons in Angular 2 forms

Having trouble displaying the values of radio button inputs in Angular 2 forms. ...

Obtain the selected portion of text value using Angular 2

In order to create a basic text editor that allows users to make selected text bold, follow these steps: Start by setting up a textarea with the value "Super text". Next, select a portion of this text (such as "Super"). But how can you retrieve the selec ...

Integrate jQuery-ui into your Angular 5 application

Having some trouble integrating jquery-ui into my Angular 5 project. I need to use a feature that requires jquery-ui, but I keep encountering this error: TypeError: WEBPACK_MODULE_10_jquery(...).droppable is not a function I initially attempted to plac ...

best way to retrieve all rows from ng2-smart table

In my application, I have implemented a smart table component that allows users to input records using default functionalities. Once the user has entered the records into the table, they then need to click on a separate "Save" button (not part of the smart ...

Steps to implement Angular routerLink on an image for seamless navigation to a different component

Is there a way to create an interactive image that leads to other sections within Angular? The intention is for this particular image to serve as a miniature profile picture of the existing user, located in the navigation bar. <img ngSrc="{{User.photo ...

Execute the "organizeImports" trigger through the terminal in TypeScript

One feature of VSCode is its editor capability to organize and clean imports in javascript and typescript files upon saving ( "source.organizeImports": true ). Inquiry Is there a way to trigger this action on a file using the command line? Something alo ...

Azure function indicates a successful status despite receiving a result code of 500

I have an Azure function containing some logic with a basic try-catch structure (code shortened). try { // perform logic here that may fail } catch (ex) { context.log(`Logging exception details: ${ex.message}`); context.res ...

Enhance your social interactions by incorporating a custom interaction field using Google Analytics

At times, I have two share buttons in the user interface of my application (depending on the state). These buttons can share the same data but are located in different parts of the UI. The goal is to analyze from which button (part of UI) the share action ...

Using TypeScript generics to constrain to either a number or a string

I am working on coding a react input component that accepts a defaultValue parameter of type string | number. This component has a state type matching the type of the received defaultValue; This is my code: type TypeName<T> = T extends number ? "nu ...

Creating a Jsonifiable type that aligns with static interfaces: A step-by-step guide

When working with Typescript, there are 3 types that share similarities as they are composed of primitive types, maps, and arrays: type Color1 = { [component: string]: number } type Color2 = { green: number red: number blue: number } interface Colo ...

The use of `super` in Typescript is not returning the expected value

Trying to retrieve the name from the extended class is causing an error: Property 'name' does not exist on type 'Employee'. class Person { #name:string; getName(){ return this.#name; } constructor(name:string){ ...

Using checkboxes to filter a list within a ReactiveForm can result in a rendering issue

I have implemented a dynamic form that contains both regular input fields and checkboxes organized in a list. There is also an input field provided to filter the checkbox list. Surprisingly, I found out that when using the dot (.) character in the search f ...

Retrieving Files from POST Request Body Using Node.js and Angular

Currently, I am developing a MEAN Stack application and facing an issue while handling a form that should allow users to upload a file upon submission. The process seems to work seamlessly on the client side; however, when I inspect the request body after ...

Issue with Angular 2: Unable to download a ZIP file as a blob

It appears that the back-end functionality is working fine, as the zip file is being created without any issues: curl -X POST -H 'Content-Type: application/json' -d '{}' http://localhost:3000/zip/create > file.zip The Django back-e ...

Avoid circular dependencies in Angular 6 to ensure proper association between modules

When working with TypeScript, how should I handle the scenario where Cat has an owner: Person Person owns a pet: Cat Cat import {Person} from './person' export class Cat { owner: Person constructor(){ this.owner = new Pers ...