Remove the cell values [undefined ] or replace with N/A

Hi Team,

I am trying the implement for one my issues relate to git-hub logs.

I have implemented and is almost working. May I know How to remove the “[undefined]” in the code of java script while send an email to the outlook .
Below is the screenshot related to it .

I would like to remove the “[undefined]” and replace it with the 0 or “N/A”.

Below is the JS which I am using it ::::

const inputArray = $input.all();

const outputMap = new Map();

inputArray.forEach((item) => {
const key = item.json.component;
const location = item.json.Location;
const count = item.json.count_Location;

if (!outputMap.has(key)) {
    outputMap.set(key, {
        count_Location: key,
        total: 0
    });
}

const existingItem = outputMap.get(key);
existingItem[location] = (existingItem[location] || 0) + count;
existingItem.total += count;

});

// Replace undefined fields with 0
const finalArray = Array.from(outputMap.values()).map(item => {
for (const key in item) {
if (item[key] === undefined) {
item[key] = 0;
}
}
return item;
});

return finalArray;

  • **n8n version: Version 1.3.1
  • **Database (default: SQLite): default: SQLite
  • **n8n EXECUTIONS_PROCESS setting (default: own, main): (default: own, main):
  • **Running n8n via (Docker, npm, n8n cloud, desktop app): n8n via Docker
  • **Operating system: CentOS 7

Thanks
DILIP BK

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

You can simply add || "n/a" after each value that may be undefined. You can do that in your code block or in an expression in a field. In the case a value is undefined or falsey, it will use the value to the right of the ||.

Example:

inputArray.forEach((item) => {
  const key = item.json.component || "n/a";
  const location = item.json.Location || "n/a";
  const count = item.json.count_Location || "n/a";
}

Then you don’t need any extra logic to change the values. You can also use || "0" if you prefer.

Hi @liam ,

Thanks for the reply. Still we see the same issue event after applying the changes ::

Please share the input data json and I will write the code to fix

Hi @liam ,
Basically, The input is the combination of the 2 things, One is with the raw data. as an example below ::

items[0].json.someArray = [
{
“repo_id”: 4251,
“repo_name”: “service”,
“component”: “ABC”
},
{
“repo_id”: 7845,
“repo_name”: “king”,
“component”: “RTS”
} ]
return items;

2nd is getting from the http request to get the repo_id with the username ::

{
“aggs”: {
“repository_project_id”: {
“terms”: {
“field”: “repository.project.id”,
“order”: {
“_count”: “desc”
},
“size”: 5000
},
“aggs”: {
“user_name”: {
“terms”: {
“field”: “user.name”,
“order”: {
“_count”: “desc”
},
“missing”: “-”,
“size”: 5000
}
}
}
}
},
“size”: 0,
“script_fields”: {},
“stored_fields”: [
“*”
],
“runtime_mappings”: {},
“_source”: {
“excludes”:
},
“query”: {
“bool”: {
“must”: [
{
“match_all”: {}
}
],
“filter”: [
{
“match_phrase”: {
“repository.project.id”: “{{ $json.repo_id }}”
}
},
{
“range”: {
@timestamp”: {
“gte”: “now-1M”,
“lt”: “now”,
“format”: “strict_date_optional_time”
}
}
}
],
“should”: ,
“must_not”:
}
}
}

Based on the 2 inputs , I am trying to get the username, I am trying to get the Location from the LDAP AD Group and below is the screenshot related to workflow.

Please help me

Please send full valid JSON

Where are the locations coming from?

What are the numbers in this table representing?

Hi @liam ,

Let me explain and provide you the input and output.

Step 1: First I am collecting the raw input and pushing it through the “code” & ItemList module . Like this I have 50 repo_id ::

items[0].json.someArray = [
{
“repo_id”: 4251,
“repo_name”: “service”,
“component”: “ABC”
},
{
“repo_id”: 7845,
“repo_name”: “king”,
“component”: “RTS”
} ]
return items;

Step 2 : I am getting the “repo_id” & “username” from the http_request using elastic search api query from the git-logs.

{
“aggs”: {
“repository_project_id”: {
“terms”: {
“field”: “repository.project.id”,
“order”: {
“_count”: “desc”
},
“size”: 5000
},
“aggs”: {
“user_name”: {
“terms”: {
“field”: “user.name”,
“order”: {
“_count”: “desc”
},
“missing”: “-”,
“size”: 5000
}
}
}
}
},
“size”: 0,
“script_fields”: {},
“stored_fields”: [
“*”
],
“runtime_mappings”: {},
“_source”: {
“excludes”:
},
“query”: {
“bool”: {
“must”: [
{
“match_all”: {}
}
],
“filter”: [
{
“match_phrase”: {
“repository.project.id”: “{{ $json.repo_id }}”
}
},
{
“range”: {
@timestamp”: {
“gte”: “now-1M”,
“lt”: “now”,
“format”: “strict_date_optional_time”
}
}
}
],
“should”: ,
“must_not”:
}
}
}
Step 3: Once I get the list of user.name from the http_request. I am separating them using itemList & set Module to get the user.name then I am pasing into LDAP AD group to get the Location using user.name :

Step 4: Then I am using merge module for the "Step1 & Step 3 " to combine the both the data using merge by position.

Step 5 : Then I am using set module to get the list of repo_id , Component & Location .

Step 6: Later I am transposing the Component into column and Location into rows to get the count.

Once I done with these steps I am sending into an email.

Currently, I am seeing [undefined] in the code module. Please help me here to set it as “0” or N/A

Thanks
DILIP BK

You need to send the JSON right before your “Code1” node.
I am not able to see what your workflow is doing to the data just from a screenshot.

Go to where you took this screenshot below and copy the JSON from the panel to the left.
I could guess with the code but that might waste both of our time, i need to see the actual input to the code I need to write

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