Fetch Random Line at a Distant Node

I have the following Workflow

it turns out that the search in the database in the bubble does not need to be done every time after the splitInBatches, it could be before since the data does not change, I would just need to execute the node function fetching the random row, but, they need to be together to work , is there any way to do with the separate nodes? example below:

So your ‘Conversas’ node returns items that you want to loop over, and each loop needs to access the same data in the ‘Companhias-Recebe’ node?

It depends on what you’re doing inside the loop and how, but if you’re using a code node you could do the following:

  • Put the ‘Companhias-Recebe’ node before the ‘Conversas’ node (both outside the loop)
  • Activate the ‘Execute once’ setting on the ‘Conversas’ node
  • In your function node inside the loop, refer to the data in ‘Companhias-Recebe’ using $('Companhias-Recebe').all()

Although to be honest if you need to combine data from the two bubble nodes if may be more elegant to use a merge node instead. But hard to say without knowing exactly what you’re trying to do.

Thank you very much for your answer, now I’m closer to getting it.

the “Conversas” node returns me 52 items I want the “Linha Aleatória” node to take a random line from the “Conversas” node the JavaScript below works if the node is immediately before it.

const newItems = [];

random_line = items[Math.floor(Math.random() * Math.floor(items.length))];

newItems.push(random_row);

return new items;

after seeing your answer I changed it to

const newItems = $(‘Conversas’).all();

random_line = items[Math.floor(Math.random() * Math.floor(items.length))];

newItems.push(random_row);

return new items;

but then it returns all items and not just a random one

how should i leave the javascript code so that it just returns a random item?

I’m guessing you simplified this code since it won’t work the way it’s written (e.g. random_line is never used).

This should work to get a random item:

const all_items = $('Conversas').all();
return all_items[Math.floor(Math.random() * Math.floor(all_items.length))];

Thank you very much, it worked perfectly

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