Javascript Methods in Functions

Is there a way to use javascript methods in functions like those used with arrays (.concat(), .join(), etc) in other javascript applications?

Whenever I attempt to use these methods I receive an error that says it is not a function.

To give this some context, I am trying to add a timestamp column to data generated by reading tags from an Allen-Bradley PLC by creating a single value array that holds the timestamp and merging that array with the array of data from the PLC. Once this data is assembled I am writing it to a CSV file.

They are available in the function node by default.
This is a standard javascript error when you try to use something like concat() on a non Buffer or Array type like a string or an undefined variable.
So the problem seems to be either with your input data or your code.
It would be helpful if you could post both, your function code and an example of the data you expect to process with it.

Johannes

Below is the code that I am testing with right now. The first is just the 'original' data set that I am generating since I do not have access to the PLC that I will be collecting data from right now. The second is the formatting function that I am trying to use.

var array = [];

array = {
    "a": 50,
    "b": 60,
    "c": 70
    
};
msg.payload = array;
return msg;
var data =[];
var data2 = [];
var date = new Date();
var name = msg.filename;
var time;
var mm;
var hh;
var ss;
if (context.get('minstor') != date.getMinutes()){
    context.set('minstor', date.getMinutes());
    msg.reset = 1;
}

if (date.getMinutes() < 10) {
    mm = "0" + date.getMinutes();
} else {
    mm = date.getMinutes();
}

if (date.getSeconds() < 10) {
    ss = "0" + date.getSeconds();
} else {
    ss = date.getSeconds();
}

if (date.getHours() < 10) {
    hh = "0" + date.getHours();
} else {
    hh = date.getHours();
}

var month = date.getMonth()+1

if (month < 10) {
    month = "0" + month;
}

var day = date.getDate();

if (day < 10) {
    day = "0" + day;
}

time = date.getFullYear()+ "-"+ month + "-" + day + " " + hh + ":" + mm +":" + ss +":" + date.getMilliseconds();
name = "Test/" + date.getFullYear() + "" + month + "" + day + "_" + hh + "" + mm + ".csv";
msg.filename = name;
var array = [];

array = {
    "TIMESTAMP": time
}
    
data = msg.payload;
var merged = [];
array.concat(data);

msg.payload = array;
return msg;

↑ you create an array then overwrite it to be an {object}

↑ again, you create an array then set it to an object.


Can you confirm the ACTUAL format of input data - and - the expected format of output data e.g...

input data...

{
    "a": 50,
    "b": 60,
    "c": 70   
}

expected output data...

[
  {
     "name": "a",
     "value": 50,
     "TIMESTAMP": "2021-02-18 12:00"
  },
  {
     "name": "b",
     "value": 60,
     "TIMESTAMP": "2021-02-18 12:00"
  },
  {
     "name": "c",
     "value": 70,
     "TIMESTAMP": "2021-02-18 12:00"
  },
]

-- or perhaps you want a lookup object instead of an array? --

expected output data...

{
  "a": {
     "value": 50,
     "TIMESTAMP": "2021-02-18 12:00"
  },
  "b": {
     "value": 60,
     "TIMESTAMP": "2021-02-18 12:00"
  },
  "c": {
     "value": 70,
     "TIMESTAMP": "2021-02-18 12:00"
  },
}

In the end the data will be fed to a csv generator to get an output as follows:

TIMESTAMP,a,b,c
2021-02-18 12:00:00.000,50,60,70

Figured out how to get there, below is where the formatting ended up.

var data;
var timestamp;
var date = new Date();
var name = msg.filename;
var time;
var mm;
var hh;
var ss;

//send reset to csv function when new file is generated
if (context.get('minstor') != date.getMinutes()){
    context.set('minstor', date.getMinutes());
    msg.reset = 1;
}


//generate timestamp and file name
if (date.getMinutes() < 10) {
    mm = "0" + date.getMinutes();
} else {
    mm = date.getMinutes();
}

if (date.getSeconds() < 10) {
    ss = "0" + date.getSeconds();
} else {
    ss = date.getSeconds();
}

if (date.getHours() < 10) {
    hh = "0" + date.getHours();
} else {
    hh = date.getHours();
}

var month = date.getMonth()+1

if (month < 10) {
    month = "0" + month;
}

var day = date.getDate();

if (day < 10) {
    day = "0" + day;
}

time = date.getFullYear()+ "-"+ month + "-" + day + " " + hh + ":" + mm +":" + ss +":" + date.getMilliseconds();
name = "Test/" + date.getFullYear() + "" + month + "" + day + "_" + hh + "" + mm + ".csv";


//add timestamp to data
timestamp = {
        "TIMESTAMP": time
    }
    
data = msg.payload;
let merged = {
    ...timestamp,
    ...data
}


//output
msg.filename = name;
msg.payload = merged;
return msg;