1

I'm having this collection of objects which are inside a html text area:

{"name":"John", "lname":"Doe","company":"example company","email":"johndoe@example.com"},{"name":"Katie","lname":"Jake","company":"example company","email":"katie@example.com"},
...
...
...

Is there a way to send the whole collection to node js and iterate through it to get values of objects?

My AJAX code:

  $.ajax({
    type: "POST",
    url: "https://example.com/nodeapp",
    data: '['+document.getElementById("list").value+']',
    success: function(data) {
      console.log(data);
    }
  });

I tried to do a foreach on req.body, but it doesn't seem to work:

var arr = req.body;

arr.foreach(element=>{
console.log(element.email);
})

Gives the error:

TypeError: arr.foreach is not a function
Shri
  • 709
  • 1
  • 7
  • 23
  • What do you see when you try to log req.body? Also, it's arr.forEach, not arr.foreach :) https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach – saglamcem Apr 04 '20 at 11:53
  • no it is not an array, https://stackoverflow.com/questions/28764822/req-body-cant-be-read-as-an-array – Dickens A S Apr 04 '20 at 11:58
  • ``` [Object: null prototype] { '[{"name":"John", "lname":"Doe","company":"example company","email":"johndoe@example.com"},{"name":"Katie","lname":"Jake","company":"example company","email":"katie@example.com"}]' : ' ' ``` @saglamcem This is what I see on console log of req.body – Shri Apr 04 '20 at 11:58
  • no it is not an array, https://stackoverflow.com/questions/28764822/req-body-cant-be-read-as-an-array – Dickens A S Apr 04 '20 at 11:58
  • may be you ca try `E` caps `forEach`, but it is not an array, you have to use `in` loop or `of` loop – Dickens A S Apr 04 '20 at 11:59
  • @DickensAS I tried E caps on forEach. Still the same result. – Shri Apr 04 '20 at 12:00
  • Then I confirm, it is not an array, use `in` loop or `of ` loop – Dickens A S Apr 04 '20 at 12:01
  • try to parse string in textarea to object before sending to server – Kevin Bui Apr 04 '20 at 12:08

2 Answers2

0

At first , you have to parse the body by using JSON.parse() function . Like this :

var arr = JSON.parse(req.body);

arr.forEach(element=>{
console.log(element.email);
});

The javascript's foreach also defined as arr.forEach(...) not arr.foreach(...) .

Ali Reza
  • 105
  • 1
  • 7
0

I found my problem! Incase someone is stuck with the same thing, the whole thing was a string:

'[{"name":"John", "lname":"Doe","company":"example company","email":"johndoe@example.com"},
{"name":"Katie","lname":"Jake","company":"example company","email":"katie@example.com"},
...
...]'

Which was considered as one property in JSON which has no value while the server received it. Pretty much like this:

{
"<<the array in string format>>" : ""
}

How I fixed this was, I pushed the objects separately into a new array and sent it to server with JSON content type. (Which was an actual array of objects)

Shri
  • 709
  • 1
  • 7
  • 23