Branch if statement help: Cannot read properties of undefined
I have a Rest API Call node at the top of my workflow. It has a query param called "api_key". For the next node, I have a branch node. I thought it would be simple to have an "if" statement with the branch.
So, in the branch node, I have this code: ctx["root"]["request"]["query"].api_key == '12345'
Then, on the true branch, I go ahead and run my next bit of code. On the false branch, I just return a message.
No matter what I do, I can't get a simple if statement to work. It says it is a javascript so I have tried to do a simple if statement in JS as well:
if (ctx["root"]["request"]["query"].api_key == 'HRauoD4QypplwAfiev7NnSeITtbIiMXX') {
return true;
} else {
return false;
}
But, that just returns an error as well. This is very confusing for a simple branch node. But, nothing seems to be working? What am I doing wrong here?
Solution:Jump to solution
@macphreak
The are a couple of issues in your workflow you submitted:
1. You are matching the condition to the wrong value due to which it is failing, the request header should match the value to which you are comparing. For example, if your request header is 123 then the match condition should be: (refer to the image below).
image.png...
4 Replies
AI Support Bot Information
<@413072678849871875> you can react on the relevant answer (message) with a ✅ in this thread when you think it has been solved, by the bot or by a human!
Anyone can react on this message with a ❌ if the GPT bot is unhelpful or hallucinating answers.
Please note: Team members will review and answer the questions on best efforts basis.
AI Response
To solve your issue with the Branch (If Else) node, you need to ensure that the condition you are writing correctly accesses the context variable and evaluates it properly. In BuildShip, the condition field in the Branch node uses JavaScript syntax, but it must evaluate to a boolean.
Here's how you can set it up:
1. Ensuring Proper Context Access: Make sure the
ctx["root"]["request"]["query"].api_key
part correctly accesses the parameter.
2. Setting Up the Condition: Use the following expression in the condition field of your Branch node:
Just like in JavaScript, ensure you're using triple equals (===
) for comparison to check both value and type.
3. Branch Node Setup: Your Branch node should have this condition set in its configuration. Ensure there are no extra spaces or syntax issues.
4. Return Nodes for True and False Cases: In the "Then" block, place the node you want to execute if the condition is true. In the "Else" block, place a Return node or any other nodes you want to execute when the condition is false.
Here’s a step-by-step example of how you should configure your workflow:
Step 1: REST API Call Node
- This node is already set up to receive the api_key
as a query parameter.
Step 2: Branch Node
- Condition:
Step 3: True Branch
- Add the node(s) you wish to run if the api_key
matches.
Step 4: False Branch
- Add a Return node or other nodes you wish to run if the api_key
does not match.
Visual Setup
Your workflow visually should look like this:
1. REST API Call Trigger
2. Branch Node
- Condition: ctx["root"]["request"]["query"].api_key === '12345'
- Then: Add your desired node(s) here.
- Else: Add a Return node with an appropriate message.
Example:
In the workflow:
#### REST API Call Trigger
Configure it to accept the query parameter.
#### Branch Node
#### True Branch
Add your main workflow nodes.
#### False Branch
Add a Return node with the following configuration:
By ensuring the correct setup as detailed above, you should resolve the issue and the Branch node will correctly evaluate the condition based on the provided api_key
.
For more information, you can refer to the official BuildShip documentation regarding Branch Node.
BuildShip AI Support
This did not work either unfortunately.
Solution
@macphreak
The are a couple of issues in your workflow you submitted:
1. You are matching the condition to the wrong value due to which it is failing, the request header should match the value to which you are comparing. For example, if your request header is 123 then the match condition should be: (refer to the image below).
image.png
2. Also adding a field in the inputs won't have any effect, you'll be required to add the query or body in the Request object, to learn more about how to use REST API Trigger - https://docs.buildship.com/trigger-nodes/rest-api and API call methods you can refer to this documentation - https://docs.buildship.com/basics/ship-an-api.
I've sent you an email with the fixed workflow. You can followup on the email.