Need help understanding input type (String or something else) to Java script, Is there a way to debug Java script?

Hi Guys,

I am not an expert of Java script but I used chat got to get a java script which will take input as value from my template column which is basically my API mutation

EX: of a row of my template column

{
      "kind": "add-row-to-table",
      "tableName": "native-table-jWEMMuChAFBW3berD6UM",
      "columnValues": {
        "Name": "ughRV70bRrmMxq59sNLtCg",
        "hUS5E": "Prajwal Devaraj",
        "3Fbda": "false",
        "WBVZ6": "false",
        "TeIW1": "false",
        "VA1A4": "ughRV70bRrmMxq59sNLtCg"
      }
    }

I am passing this column as p1 to my java script below

// Global array to hold the combined column values from all rows
let jsonArray = [];

function processCreatorAPIMutation(p1) {
  try {
    // Check if p1 is a string (i.e., needs to be parsed) or an object
    let rowObject = typeof p1 === "string" ? JSON.parse(p1) : p1;

    // Check if the rowObject has 'columnValues', and push its value to the global jsonArray
    if (rowObject && rowObject.columnValues) {
      jsonArray.push(rowObject.columnValues);
    }
  } catch (error) {
    console.error("Error parsing JSON:", error, "Input data:", p1);
  }

  return jsonArray; // Return the combined array after each invocation
}

// The variable 'p1' will be automatically passed from Glide.
// The global jsonArray will accumulate the results from each row.
let result = processCreatorAPIMutation(p1);
console.log(result); // This will output the cumulative JSON array of column values

Multiple tries lead me to this script which gives me the following error

Screenshot 2024-09-05 at 10.52.28 AM

Want to know what is the input which is given as p1, is it a string value that is sent internally by glide?

Regards,
Dilip

Your function doesn’t use a return line to actually return something to the column. Your last two lines can just be:

return processCreatorAPIMutation(p1)

Also, it looks like you’re trying to return an array there. You should turn that into a string before doing the return line.

Thank you replying @ThinhDinh

user Chat GPT to make the changes you asked

// Global array to hold the combined column values from all rows
let jsonArray = ;

function processCreatorAPIMutation(p1) {
try {
// Check if p1 is a string (i.e., needs to be parsed) or an object
let rowObject = typeof p1 === “string” ? JSON.parse(p1) : p1;

// Check if the rowObject has 'columnValues', and push its value to the global jsonArray
if (rowObject && rowObject.columnValues) {
  jsonArray.push(rowObject.columnValues);
}

} catch (error) {
console.error(“Error parsing JSON:”, error, “Input data:”, p1);
}

// Convert jsonArray to a JSON string before returning it
return JSON.stringify(jsonArray); // Return the combined array as a string after each invocation
}

// The variable ‘p1’ will be automatically passed from Glide.
// The global jsonArray will accumulate the results from each row.
return processCreatorAPIMutation(p1);

It still returns me only the corresponding row value as JSON object.

Doesn’t return me all rows combined value as a string.

Regards,
Dilip

What do you mean by “all rows combined”? It would only run on its own row.

Hi @ThinhDinh ,

I understand now that it processes only single row and outputs the rows values.

But it does not iterate through every array to return me the JSON array of all items values.

Regards,
Dilip