0

In my Node-RED function, I've been struggling to handle the msg.payload and extract a specific component of the json involved with it.

Here's the code

var js = msg.payload;
var output = {};
 //var Ids = js.events.sum;//.value;
 //var labels = js.externalIds.Id;
 js.forEach(function(sum){
    output.push(js.events.sum);
})

msg.payload = output;
console.log(msg.payload);
return msg; 

I've tried store a "JSON.parse()"-d variable of the msg and msg.payload, but each time I received an 'unexpected o' error, which I've been informed means something was parsed redundantly.

I've been looking at

forEach is not a function error with JavaScript array

forEach is not a function error

SyntaxError: Unexpected token o in JSON at position 1

But these haven't clarified how to handle msg objects.

The following have also been helpful for other causes but not for figuring out why my js variable cannot 'foreach':

https://nodered.org/docs/user-guide/messages

https://nodered.org/docs/user-guide/writing-functions

The exact error is TypeError: js.forEach is not a function

Clarification appreciated.

Edit

I'm trying to select data from an api call that returns JSON of multiple instances of the same types data (different values). From those instances of the same types, I'm trying to select the returned values of one type.

For example,

Let's say the returned json is structured like this:

{
   "tupperware": [
       {
            "spoon": "salad spoon",
            "fork": "soup fork"
       },
       {
            "spoon": "soup spoon",
            "fork": "cake fork" 
       }
      ],
}

And I'm trying to select only the fork values. How would I go about doing that?

DeciJ
  • 21
  • 7

1 Answers1

0

{}.forEach is not a function because, well, it's not. I think you want a for in loop.

Other issues:

  1. Your output variable is an empty object, but you're trying to push to it. That won't work. That should be an array.
  2. In your current forEach loop (that won't work) you are trying to push js.events.sum. I think you just need to push sum but it's unclear what you're actually after.

Here is an example of a for in loop.

const myObj = {test: 1};

for(const el in myObj) {
    console.log(myObj[el]);
}

Your code:

var js = msg.payload;
var output = []; // This should be an array if you intend `push`ing to it.
 for(const el in js) {
    output.push(js[el]); // Not exactly sure what you're trying to push here
 }

msg.payload = output;
console.log(msg.payload);
return msg; 

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in

mwilson
  • 12,295
  • 7
  • 55
  • 95
  • I appreciate your answer, I'm trying to get my head around it, but if I'm parsing JSON and only want to get one property from multiple lists instead of operating on properties, is this the way to go? – DeciJ Aug 05 '19 at 19:19
  • If you're trying to iterate over an object, you have to use `for ... in ...`. Otherwise, you'll need to convert your object to an array in order to iterate over it using `forEach` or any other iterable methods. Please remember to upvote/mark as answer – mwilson Aug 05 '19 at 19:21
  • Thank you for your answer, but I've found I'm trying to iterate over specific values in a **series** of objects within JSON. Your answer is excellent, but does not completely satisfy the question, which is my fault for not asking properly. – DeciJ Aug 05 '19 at 20:55
  • I just found out about the Object.keys() abstraction. – DeciJ Aug 06 '19 at 13:08