function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Mahendiran Jayavarma 24Mahendiran Jayavarma 24 

forEach is not a function

Hi Friends,

I am getting error 'forEach is not a function' error during the page load. Please advise if you have time. Thank you for the help.

Component:

<aura:attribute name="placement"      type="Placement__c" default="{'sobjectType':'Placement__c'}" />
<aura:attribute name="salesOrgCountryCode"          type="String" default="GB"  />

  <aura:iteration items="{!v.errors}" var="error" indexVar="index">
                    <h2>{!error}</h2>
    </aura:iteration>

Component controller.Js

initialise : function( component, event, helper ) {
        helper.cloneRatesHelper(component, event);
        helper.isReadOnly( component )
            .then( function( aPlacements ) {
                component.set( 'v.placement', aPlacements[0] );           
                component.set( 'v.salesOrgCountryCode', aPlacements[0].Placement_Header__r.Sales_Org__r.RegisteredCountryCode__c );      I have null value on this record.
                helper.setupEcmOptions( component );
                return helper.getRegionsList( component );
            } )
           
            .catch( function( error ) {
                helper.handleErrors( component, error );
            } );

    },


Helper Class:

handleErrors: function( component, aErrors ) {

        if ( typeof aErrors === 'string' ) {
            var error = { message : aErrors };
            aErrors = [ error ];
        }

        var aComponentErrors = component.get( 'v.errors' );
        aErrors.forEach( function( error ){                      aErrors.forEach is not a function] Failing descriptor: {markup://c:RateComponent}
            if( error.message ){
               aComponentErrors.push( error.message )
            }

            if( error.pageErrors ) {
                error.pageErrors.forEach( function( pageError ) {
                    aComponentErrors.push( pageError.message );
                } );
            }

            if( error.fieldErrors ) {
                error.fieldErrors.forEach( function( oError, fieldName ) {
                    oError.forEach( function( errorList ) {
                        aComponentErrors.push( "Field Error on " + fieldName + " : " + errorList.message );
                    } );
                } );
            }
        } );
        component.set( 'v.errors', aComponentErrors );
    },
 
David Zhu 🔥David Zhu 🔥
you may use  "for (var s in aErrors) {} " to do the iteration.
Mahendiran Jayavarma 24Mahendiran Jayavarma 24
Thank you David for the reply. I have just corrected. Can you pelase review. I am new to Jquery 

handleErrors: function( component, aErrors ) {

         if ( typeof aErrors === 'string' ) {
            var error = { message : aErrors };
            aErrors = [ error ];
        } 
        var aComponentErrors = component.get( 'v.errors' );          
  for( function( var error in aErrors  ){             

            if( error.message ){
               aComponentErrors.push( error.message )
            }
         } );
        component.set( 'v.errors', aComponentErrors );
    },
 
David Zhu 🔥David Zhu 🔥

handleErrors: function( component, aErrors ) {
         if ( typeof aErrors === 'string' ) {
            var error = { message : aErrors };
            aErrors = [ error ];
        } 
        var aComponentErrors = component.get( 'v.errors' );          
  for( var s in aErrors  ){             
               aComponentErrors.push( aErrors[s] )
         } ;
        component.set( 'v.errors', aComponentErrors );
    },
Mahendiran Jayavarma 24Mahendiran Jayavarma 24
Still I get the error. How should i change another two foreach 

 handleErrors: function( component, aErrors ) {

        if ( typeof aErrors === 'string' ) {
            var error = { message : aErrors };
            aErrors = [ error ];
        }

        var aComponentErrors = component.get( 'v.errors' );
         for( var s in aErrors  ){             
           
                aComponentErrors.push( aErrors[s] )
            }

           
                error.pageErrors.forEach( function( pageError ) {
                    aComponentErrors.push( pageError.message );
                } );
            }

            if( error.fieldErrors ) {
                error.fieldErrors.forEach( function( oError, fieldName ) {
                    oError.forEach( function( errorList ) {
                        aComponentErrors.push( "Field Error on " + fieldName + " : " + errorList.message );
                    } );
                } );
            }
        } ;
        component.set( 'v.errors', aComponentErrors );
    },