Variable Sized Zero Filled Array Creates 'Undefined' Object

I'm trying to create an array of a certain length filled with 0. I used this code to do it:

// Create the "conditionMet" object to determine if the recipes conditions have been met by the triggers
msg.conditionMet = new Array(msg.myRecipes.length).fill(0)

The array seems to initialize correctly as seen through the debug node:

image

Then when I try to modify the values through a switch statement (part of it here):

for (i = 0; i < msg.myRecipes.length; i++){
    // Gets the current trigger condition to pass in the switch statement
    //var triggerCondition = msg.triggerConditionArr[i];
    var triggerCondition = msg.myRecipes.r_Trigger[i];
    
    switch(msg.triggerCondition){
        // Half Ranges
        case "percentage-S-50":
            if (potentiometerVal <= 50){
                msg.conditionMet[i] = 1;
            }
            break;
        case "percentage-G-51":
            if (potentiometerVal >= 51){
                msg.conditionMet[i] = 1;
            }
            break;
        // Increments of 10 ranges
        case "percentage-B-0-9":
            if (potentiometerVal >= 0 && potentiometerVal <= 9){
                msg.conditionMet[i] = 1;
            }
            break;
        (.... switch statement continues here)

I get the following error:
image

I'm not sure how else to create the array...

That code is correct, so the issue will be somewhere else in your flow.

The first debug screenshot you have shared is showing is msg.triggerConfitionArr - which is a different property to the one in the initial code - msg.conditionMet. So that doesn't tell us anything about the original array.

In your bigger piece of code, you configure the for loop to iteration from 0 to msg.myReceipes.length. But in the next line you access msg.myRecipes.r_trigger[i].

Without knowing the structure of your message, I can't say what it should be - but I can tell you what you have currently is wrong.

1 Like

You're right, the arrays were actually created correctly! I editted the question accordingly. I also understand about the line:

var triggerCondition = msg.myRecipes.r_Trigger[i];

It was structured as follows before and it gave the same error message (the first line is outside the for loop)

// Extract the trigger conditions in an array 
msg.triggerConditionArr = msg.myRecipes.r_Trigger;

var triggerCondition = msg.triggerConditionArr[i];

Basically I'm trying to extract the different recipe triggers to compare in the switch statement in each itteration of the for loop:

In which case, I think you need to change the opening lines of the loop as follows (I've made it more verbose than it needs to be, but hopefully that helps make it clearer)

for (i = 0; i < msg.myRecipes.length; i++){
    // Get the recipe
    var recipe = msg.myRecipes[i];
    // Get the r_trigger for this recipe
    var triggerCondition = recipe.r_Trigger;
    // Use that value in the switch:
    switch(triggerCondition){

Yeah, I see what the problem was. Thanks a lot !!

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