Unable to process multiple output in Code node despite choosing “Run once for each item”
What is the error message (if any)?
ERROR: Unknown top-level item key: companyName [item 0]
Access the properties of an item under .json, e.g. item.json
Please share your workflow
// Get the input data
let inputData = $items('Set')[0].json.companyName;
let processedData = inputData.replace(/\s/g, '').toLowerCase();
return {"companyName": processedData};
(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)
Share the output returned by the last node
Input Data - {
“companyName”: “Macwel leadtech LLC”
}
Output Data must have:
Remove URL
Convert all characters to lowercase
Remove symbols
Remove common naming conventions (remove : LLC/Corp/Inc)
Remove leading and trailing whitespace
Capitalize the first letter of each word
It would be great if someone can help me update the code.
You may want to try let inputData = $('Set').item.json.companyName; or just $input.item.json.companyName; if it’s coming as output from the previous node. Hope that works anyway
Sorry to hear you’re having trouble with this - following up from @jonflow 's reply, the “run once for each” option is quite strict in the data it expects. It’ll be wanting data in this format:
Hi @EmeraldHerald thank you for the response. I get it, I will have to construct a Json payload to execute multiple items. But where I am stuck exactly is, I am pushing multiple values from a column in Gsheet as Input value. In this case my I cannot construct a Json payload since I may not know how many items I will be processing in a single execution. Attaching my workflow screen shot for reference.
Hi @Jon@EmeraldHerald@jonflow I tried and made it work. So the updated script can process multiple items now that will “run once for all items”.I appreciate your quick support on this
Here is the modified script that I used;
// Get the input data
let inputData = $items('Set2') // change this to the proper set to pull input value
let outputData = []
for (let i = 0; i < inputData.length; i += 1) {
let inputDataItem = inputData[i].json.location;
// split the string by comma and get the first word
let processedData = inputDataItem.split(',')[0].trim();
// remove extra whitespaces and non-alphabetic characters
processedData = processedData.replace(/[^a-zA-Z ]/g, '').trim();
let outputDataItem = {}
outputDataItem["location"] = processedData;
outputData.push(outputDataItem)
}
return outputData;
If the Set2 node is the node being used before the Code node you could also use the loop example we load the node with so you would end up with something like this…
let outputData = [];
for (const item of $input.all()) {
outputData.push( {
'location': item.json.location.split(',')[0].replace(/[^a-zA-Z ]/g, '').trim()
});
}
return outputData;
Ignore the location setting… The for loop is the important bit there so you can see how you could use it.