Node.send( ) stuck with loop

Given:
msg.payload = 0
and
msg.menu exists thus:

{"1":"Show clock/time","2":"Show alarms","3":"Edit time/date","4":"Edit alarm/s","5":"Alarm run time"}

I have done things similar and done things like this before.
They work.
I'm not getting this.

So I have a function node with this code:

var starting = parseInt(msg.payload);
var ending = starting + 2     // set to 2 for now.   Just to prove the code set at 2

//node.warn("Starting " + starting)
//node.warn("Ending " + ending)
//node.warn(msg.menu)

if (starting < ending)
{
    //
    pop()
}
return;

function pop() {
    node.warn("Loop" + starting);
    node.warn("Ending " + ending);
    node.send(msg.menu[starting]);
    starting +=1;
    if (starting < ending){
        pop()
    }
}

Yeah, full of node.warn() stuff.

I'm not getting why the node.send(msg.menu[starting]); isn't working.

When the node.warn() line is active I see the structure.

And I am getting this as an error:
Function tried to send a message of type string

node.send needs to send an object, unless you configured the debug node to display something else. You are not sending an object, but a number eg. node.send({menu:starting}) - which in turn will output {menu:x}

Ideally, it should send one (two) of the entries.

Yes, I realised that 0 is not a good starting point.

So, that aside.... I am not getting why it is complaining.

I really am not getting these types.
(Some one shoot me)

Trying to de-construct it as much as I can:

var x = msg.menu[1];
node.warn("X " + x);
node.send(x);
var y = msg.menu[2];
node.warn("Y " + y);
node.send(y);

I get the X and Y messages shown to me.

So..... I'm not seeing the elephant.

Oh! So it has to be part of a message?

eg: node.send(msg.something)

That doesn't hold water because I tried to send msg.menu[1] and it didn't like it either.

You have to send a message Object.

You cannot send just a String.

So instead of doing

node.send(msg.menu[1]);

you need to wrap it in a message object:

node.send( { payload: msg.menu[1] })
1 Like

One day I may remember all these structures.

Alas until then, sorry folks.

Nick: Thanks.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.