# Javascript Methods in Functions

**URL:** <https://discourse.nodered.org/t/javascript-methods-in-functions/41209>\
**Category:** General\
**Created:** [18 February 2021 01:12 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209 "2021-02-18T01:12:45Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AutomationExpert](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@AutomationExpert](https://discourse.nodered.org/u/AutomationExpert)\
**Post date:** [18 February 2021 01:12 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/1 "2021-02-18T01:12:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [18 February 2021 06:04 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/2 "2021-02-18T06:04:38Z")

</div>

> [@AutomationExpert](#):
>
> that says it is not a function

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

---

<div class="post-metadata">

**Author:** ![AutomationExpert](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@AutomationExpert](https://discourse.nodered.org/u/AutomationExpert)\
**Post date:** [18 February 2021 13:28 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/3 "2021-02-18T13:28:28Z")

</div>

> [@JGKK](#):
>
> > [@AutomationExpert](#):
> >
> > that says it is not a function
> 
> 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.

```auto
var array = [];

array = {
    "a": 50,
    "b": 60,
    "c": 70
    
};
msg.payload = array;
return msg;

```

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [18 February 2021 13:39 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/4 "2021-02-18T13:39:56Z")

</div>

> [@AutomationExpert](#):
>
> ```auto
> var array = [];
> 
> array = {
> "a": 50,
> "b": 60,
> "c": 70
>     
> };
> msg.payload = array;
> 
> ```

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

> [@AutomationExpert](#):
>
> ```auto
> var array = [];
> 
> array = {
> "TIMESTAMP": time
> }
>     
> data = msg.payload;
> var merged = [];
> array.concat(data);
> 
> ```

↑ 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...

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

```

#### expected output data...

```auto
[
  {
     "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...

```auto
{
  "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"
  },
}

```

---

<div class="post-metadata">

**Author:** ![AutomationExpert](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@AutomationExpert](https://discourse.nodered.org/u/AutomationExpert)\
**Post date:** [19 February 2021 00:35 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/5 "2021-02-19T00:35:37Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![AutomationExpert](https://avatars.discourse-cdn.com/v4/letter/a/13edae/32.png) [@AutomationExpert](https://discourse.nodered.org/u/AutomationExpert)\
**Post date:** [19 February 2021 00:56 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/6 "2021-02-19T00:56:43Z")

</div>

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

```auto
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;

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [5 March 2021 00:56 UTC](https://discourse.nodered.org/t/javascript-methods-in-functions/41209/7 "2021-03-05T00:56:58Z")

</div>

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