0

I am trying to connect a web service method to my lightning-datatable. The idea is when the web service is called, my data table will display the returned information. However I am getting an error due to the wire service and HTTP Get method.

I used the following link as a template for my lwc: https://www.salesforcecodecrack.com/2019/10/display-reference-data-in-lwc.html

Currently I get the following error:

[LWC component's @wire target property or method threw an error during value provisioning. Original error:
[t.forEach is not a function]]

HTML:

<template>
    <lightning-card title="Static Data" icon-name="standard:record">  

        <lightning-datatable columns={columns} 
                            data={data} 
                            key-field="id"
                            hide-checkbox-column="true"
                            show-row-number-column="true"></lightning-datatable>
    </lightning-card>
</template>

JS:

import {LightningElement, track, wire} from 'lwc';
import getData from '@salesforce/apex/WebServiceClass.getMethod';

const columns = [
    {
        label: 'column1',
        fieldName: 'column1', 
        type: 'text',
    }, {
        label: 'column2',
        fieldName: 'column2',
        type: 'date'
    }, {
        label: 'column3',
        fieldName: 'column3',
        type: 'text'
    }
];

export default class myLWC extends LightningElement {
    @track data = [];
    @track columns = columns;

    @wire(getData)
    opp({error, data}) {
        if(data) {

            let currentData = [];

            data.forEach((row) => {
                let rowData = {};
                if(row.Vouchers){
                    rowData.column1 = row.column1;
                    rowData.column2 = row.column2;
                    rowData.column3 = row.column3;
                else{
                    window.console.log("No data");
                }
                
                currentData.push(rowData);
            });

            this.data = currentData;
        }
        else if(error) {
            window.console.log("Error: " + error);
        }
    }
}

Apex:

@AuraEnabled(cacheable=true)
@HttpGet
global static InnerClass getMethod(){
   InnerClass c = new InnerClass(); 
    return c;
}

// Inner class represent return response
global class InnerClass{        
}

EDIT:

To fix this I just removed the for each from my js since I was returning an object not an array

1 Answer 1

1

forEach works only on arrays, but you are returning an object.

Your code is:

global static InnerClass getMethod(){
   InnerClass c = new InnerClass(); 
    return c;
}

I think you need something like this.

global static List<InnerClass> getMethod(){
    //...
}

Also please confirm if really you need global access for the method, the public should be fine.

1
  • 1
    Thanks I will try it out
    – M guy
    Jul 30, 2020 at 12:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .