Node flow recursive

I am beginner to Node-red, i have doubt how can i call nodes recursively?

example: i have 100 employees for each employee i have attendance available in db, this time i have to loop through 100 records and get their attendance, how can i process record one by one and get data

Why would you want to do that?

1 Like

You should be able to do that in a single step. At worst, you'd only need a loop not recursion.

Hi @akhilendharreddy

it is hard to provide anything very specific because there is not much detail in your question.

Lets assume you have those records in a database. There is probably a node available for the particular database technology you're using and you can probably configure that node to return that list of employees and associated data. That may get returned as a single message with all of the data in an array, or it may send one message per record - it all depends on which node you use and what functionality it provides.

If it returns all records in a single message, probably an array, you could then use a Split node to turn that into one message per record - at which point you can pass each of those messages through whatever flow you want to process it.

Hopefully that gives you a bit of a direction to follow. If you want to share some more specific details, then we can probably provide some more specific help.

2 Likes

Thanks for contributing, I got what your trying to say, as you mentioned i got array of records for db, here again i have query db for each record, which means i have to loop through the array of records for each time query will vary ex: select * from student where student_id = {{1,2,3}}, how can i achieve this?

@akhilendharreddy where do you get that list of student ids to begin with? Do you do one query of the database to get that list, and then you want to do another query to get the details of each student in turn?

Or is that list hardcoded somewhere? Or does it come from another source...?

You still have not mentioned what database you are using or what nodes you are using, so it is still hard to be very specific.

@knolleary i am using mysql db and using mysql node, i will do one query to get all student id's then forEach student_id i have to query some other details, so query will be loop each time student_id changes, usually mysql node execute query gives result set right? i my condition i have to execute mysql node in loop.

@akhilendharreddy so the main thing to do here is to stop thinking about it in terms of a loop.

As I described in my original reply, you can use the split node to turn a single message returned by the mysql query node into a stream of multiple messages, one for each element in the result from the query:

[Inject] -> [MySQL Query] -> [Split] -> [... something ...] -> [Mysql Query] -> [... something ...] 

This means if the first MySQL query node returns a message with a payload of [1,2,3,4], then when you pass that through the Split node, you'll get a sequence of messages:

   {payload: 1}
   {payload: 2}
   {payload: 3}
   {payload: 4}

Each of those messages will pass down the rest of the flow, so you can do whatever processing you want for the individual student ids.

@knolleary thanks really helpful

@akhilendharreddy Are you quering a table to get all the student ID, tehn for each student quering the same table? If so, why not query the table for all the ID AND retreive the other columns you need at the same time?

For example if you are doing this:
select student_ID from STUDENTS;
then for each row returned you do this:
select (f_name, l_name, status) from STUDENTS where student_ID = nn;
where nnnn= the studentID from the first select,

why not do this:
select (student_ID, f_name, l_name, status) from STUDENTS;
and save yourself a lot of extra DB calls?