0

While using for each, i am getting the error "unhandledRejection: TypeError: data.forEach is not a function"

Solution i tried I converted the data into JSON format before using for each

const data = JSON.parse(message);

I logged the JSON message and below is the message:

{
  '0': {
    'Id': 4680,
    ’Flgtrue ?': 'Yes',
    'Type': ‘rrrr’,
    RN: '56971',
    'Name': ‘TSN’’s Percussive Arts Centre.Inc’,
    'Start Date': '2022-01-01',
    'End Date': '2023-08-02',
  },
‘1’: {
    'Id': 4681,
    ’Flgtrue ?': ‘No’,
    'Type': ‘rrsrr’,
    RN: '56975’,
    'Name': ‘TSN’’s Percussive Arts Centre.Incffff’,
    'Start Date': '2022-01-01',
    'End Date': '2023-08-02',
  },
  letter_path: ‘Test/File/2050_Sample.pdf'
}

Any help is appreciated

Thank You in Advance

  • 2
    `data` is not an array. "Normal" objects do not have a `forEach` method. – InSync May 30 '23 at 17:56
  • @InSync Is there any way i can use to loop through the sample message i have provided. – T S Nandakumar May 30 '23 at 17:59
  • You have an object, not an Array. `forEach` is for arrays. You should iterate through `Object.keys`. – Scott Marcus May 30 '23 at 17:59
  • 1
    See [How to iterate over a JavaScript object?](https://stackoverflow.com/questions/14379274/how-to-iterate-over-a-javascript-object). – InSync May 30 '23 at 18:00
  • @InSync That accepted answer in that post uses very old technique and, while it will work, `for/in` isn't really the best approach. Instead it should be `Object.keys`. Below the accepted answer shows this. – Scott Marcus May 30 '23 at 18:00
  • @ScottMarcus There are multiple answers about that at the question I linked. Nevertheless, this is a duplicate. – InSync May 30 '23 at 18:02
  • Where is your data coming from? If you have control over this, I'd suggest fixing your data to return an array, rather than an object with some sequential keys and some not. Having to use `Object.keys`/`.values`/`.items` to iterate objects is typically an antipattern. Objects are designed for lookups, not iteration. Combining iteration and key-based lookup designs into a single object is another red flag. Use one or the other and add nesting as necessary. – ggorlen May 30 '23 at 18:09

1 Answers1

0

Your code contains typographic quotation marks. These are invalid in JavaScript. Also, as mentioned before, you need to turn the data object into some kind of iterable structure, like an array. One way of doing this would be to use Object.values(data):

const data = {
  '0': {
    'Id': 4680,
    'Flgtrue ? ': 'Yes',
    'Type' : 'rrrr',
    RN: '56971',
    'Name': 'TSN\'s Percussive Arts Centre.Inc',
    'Start Date': '2022-01-01',
    'End Date': '2023-08-02',
  },
  '1': {
    'Id': 4681,
    'Flgtrue ?': 'No',
    'Type': 'rrsrr',
    RN: '56975',
    'Name': 'TSN\'s Percussive Arts Centre.Incffff',
    'Start Date': '2022-01-01',
    'End Date': '2023-08-02',
  },
  letter_path: 'Test/File/2050_Sample.pdf'
}

Object.values(data).forEach(el=>console.log(el.RN||el))
Carsten Massmann
  • 26,510
  • 2
  • 22
  • 43
  • *you need to turn the data object into some kind of iterable structure, like an array* <-- An object is already an iterable structure. Iterating over it with `Object.keys()` is the most direct approach. – Scott Marcus May 30 '23 at 18:03
  • Thanks @Carsten Massmann , It worked , Marking this as an answer - Thank You! – T S Nandakumar May 30 '23 at 18:13
  • @ScottMarcus Objects are *not* iterable by definition since they don't implement an [iteration protocol](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol). – InSync May 30 '23 at 19:02
  • @InSync I think you may be splitting hairs. There really is no need to convert the object when `Object.keys` exists. – Scott Marcus May 30 '23 at 19:06
  • @ScottMarcus Maybe, but that `Object.keys()` and similar methods exist has nothing to do with objects not being iterable (and that is exactly why they exist: to provide an iterable for key-value pairs). – InSync May 30 '23 at 19:08