AngularJS foreach not able to find function within same Service

angularjs foreach array of objects
foreach loop in angularjs
foreach angular 7
angular 6 foreach example
javascript foreach
angular foreach index
for loop in angularjs
for loop in angular 7

need some help, for the life of me I can't get this to work. I'm trying to loop through an array which is within a config value and then trigger another function within the service I created, but I get an error

TypeError: Cannot read property 'registerToken' of undefined

Is there not a better way to do this it feels very clunky?

CampaignService.ts

import { ServerConfig } from "../core/ServerConfig";

declare var config: ServerConfig;

export class CampaignService {
  static $inject = ["localStorageService"];

  constructor(
    public localStorageService: angular.local.storage.ILocalStorageService
  ) {

  }             

  launchCampaign(campaignToken: string) {
    if (campaignToken) {
      config.campaigns.forEach(function (data) {
        if (campaignToken == data.title) {
          if (data.enabled) {
            this.registerToken(campaignToken);
          }
        }
      });
    }
  };

  registerToken(campaignToken: string) {

  }    

}

You need to use an arrow function which have the context,

 config.campaigns.forEach((data) => {
        if (campaignToken == data.title) {
          if (data.enabled) {
            this.registerToken(campaignToken);
          }
        }
 });

angular.forEach, Invokes the iterator function once for each item in obj collection, which can be forEach does not iterate over inherited properties because it filters using the  You should not use service to manipulate DOM element. You should manipulate DOM only at directives. Your problem is you DONT have anywhere to listen to TEXTAREA SELECTION EVENT and your service will not update the data inside.

After some investigation I found this to be the best shorthand might be wrong, comments welcome

import { ServerConfig } from "../core/ServerConfig";

declare var config: ServerConfig;

export class CampaignService {
  static $inject = ["localStorageService"];

  constructor(
    public localStorageService: angular.local.storage.ILocalStorageService
  ) {

  }             

  launchCampaign(campaignToken: string) {
    if (campaignToken) {
       (config.campaigns.find(function (obj) { return obj.title === campaignToken; })) ? this.registerToken(campaignToken) : void (0);
    }
  };

  registerToken(campaignToken: string) {

  }    

}

TypeError: "x" is not a function, The JavaScript exception "is not a function" occurs when there was an attempt to call a value from a function, but the value is not actually a function. forEach() and Set.prototype.forEach Sometimes when making a class, you may have a property and a function with the same name. See also. Functions  Angularjs Foreach(angularjs.foreach)- angularjs.foreach is used to iterate object collection items which can be an object or an array. Here in this tutorial we are going to explain how you can use foreach loop in AngularJs. You can use our online demo try and edit the code online.

Another way of doing it will be passing outer "this" to the forEach function, then the foreach have "this" context defined.

Something like:

config.campaigns.forEach(function (data) {
    if (campaignToken == data.title) {
      if (data.enabled) {
        this.registerToken(campaignToken);
      }
    }
}, this);

You can refer the documentation of foreach here.

Working with Iteration Methods in JavaScript Arrays, In this tutorial, we will use iteration methods to loop through arrays, The forEach() method calls a function for each element in an array. to the console. map() does not mutate the original array, it instead returns a new array value. We can use the same seaCreatures example from the find() method. angular.forEach is actually a completely synchronous operation, so returning a promise doesn't make a lot of sense here. angular.forEach is actually primarily used as a polyfill for es5 forEach within the framework itself, and it has been stated that it was probably a mistake to export it.

The 10 Most Common Mistakes JavaScript Developers Make, Uncaught TypeError: undefined is not a function Although this is true in many other languages, it is not true in JavaScript. would be important that they both get the same object, even if priorThing gets assigned to over and over, Code errors that would otherwise have been ignored or would have failed silently will now  You can loop through an Array or an Object in AngularJS using the forEach () function. The function invokes the iterator function that iterates or loops through each item in an array. I am sharing two examples here, showing how to use the forEach () loop in AngularJS to extract items or values from array or an object. forEach () Syntax.

48 answers on StackOverflow to the most popular Angular questions, @Directive v/s @Component in Angular; Angular HTTP GET with TypeScript error http.get(…).map is not a function in [null]; How to use jQuery with Angular? Observable provides operators like map , forEach , reduce , similar to an array In AngularJS, I am able to use filters (pipes) inside of services and controllers  Creating Services. Application developers are free to define their own services by registering the service's name and service factory function, with an AngularJS module. The service factory function generates the single object or function that represents the service to the rest of the application.

AngularJS, That is, // future requests to authHttp service return the same instance of authHttp servicesModule.factory('authHttp', function(Shttp, request that requires authorization will be made via authHttp . get() instead of You should be able to just copy the code right out of here, paste into your application, and make it work for you. The find () method executes the function once for each element present in the array: If it finds an array element where the function returns a true value, find () returns the value of that array element (and does not check the remaining values) Otherwise it returns undefined. Note: find () does not execute the function for empty arrays.

Comments
  • Awesome thanks @sajeetharan small things that had me for hours. I see this also seems to work var obj = config.campaigns.find(function (obj) { return obj.title === campaignToken; });
  • Great if that helped