Expressions on multiple objects in node output

Hello N8N Team,

I have been playing around with n8n a little bit and trying to make a new node. I am trying to connect 2 nodes together by using expressions. The problem that I am facing is that the expression editor only shows 1 object from the output of the first node in the expression editor. Here are the diagrams of what I am trying to do.

Workflow looks something like

The output of the first node is

I am connecting this to the 2nd node and trying to use the expressions to take data but it only shows 1 object from the output of the first node


Note that only 1 object from output of mongodb node is showing whereas the node had an output which contained 2 objects.

Is this a limitation or I am doing something wrong? How can expressions be used for multiple objects in the output of nodes.

Hey @Talha_Khan,

The expression just shows the first item but when you run the workflow it will “call” the Slack node as many times as items are in the input.

Let me know if you have any further questions.

I am not sure if I fully understand this. The slack node(or any other node) implements the execute method. My understanding is that it will only be called once per workflow invocation. I have even tested it. The method is only called once.

Sorry If I was not clear.

Will be called once but behind scenes, the Slack API would be called as many times as inputs the node has.

Example:

As you can see the Slack Node is receiving 2 items as input, meaning I will receive 2 messages on slack once the workflow runs.

If your case you are gonna receive as many messages on slack as the mongo find function returns.

Let me know if that helps.

@Talha_Khan

In your snaphot of slack node, both values testimg and message1 belong only to 1 object from the output. You can use unique values in each object to verify what I am saying. Can you show the snapshot of the expression editor. I only see 1 object again. Here it is

Yeah, it will show always the first value since the expression is just a reference.

If you run the workflow you are going to realize what I’m telling.

Run it and let me know.

1 Like

Hey @Talha_Khan,

Let me know how it goes. I’m happy to help.

@Talha_Khan I’m reading your first comment again. I want to make sure I did not misunderstand your question.

You have the two records that MongoDB returns. What do you want to do with that data?

Send a message for each record to slack?

Pick just one record out of those two and send just one message to slack?

Regards.

Hey @RicardoE105 Thanks for swift replies.

I want to use expressions in my node to process multiple objects coming to it in the input. I see your point now. Looking at the code for slack node, i see the following code

for (let i = 0; i < items.length; i++) {
			let endpoint = '';
			body = {};
			qs = {};

			resource = this.getNodeParameter('resource', i) as string;
			operation = this.getNodeParameter('operation', i) as string;

I think the main thing here is the use of index i in the method this.getNodeParameter which uses the expression from each item ? Is this correct understanding now?

You got it @Talha_Khan

Bravo!! Thanks! You guys are amazing! Really responsive and extremely helpful!

1 Like

Enjoy n8n and please feel free to contact me if you need any further information.

Regards.

Before I post a new question… I thought I’d ask here: How do I combine the results? I want to take 9 results and post to slack exactly ONCE with a list of the 9 things.

Wow, that is simple. Straight JS function makes sense. Thanks jan