# I need help again, problem with Object array with 3 values into a function node

**URL:** <https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164>\
**Category:** General\
**Created:** [5 March 2024 16:41 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164 "2024-03-05T16:41:59Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [5 March 2024 16:41 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/1 "2024-03-05T16:41:59Z")

</div>

Hi,

What am I doing wrong, I have wasted hours trying to find out !  
  
**I only want the object array with 3 values to go into my database once**.

I have an object array with 3 values

![Object Array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30a376b09987e8031b28d3d102de839b4e91f39a.png)

I also have another object with 1 value.  
 ![batt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/d/1d03ee1f4b68304c896a39b954baf43eb2854fc8.png)  
  
  
  
Image **No1** works but I get the same data 3 times in my database.

Image **No2** works I get the data only once in my database ...  
BUT i get this error in the debug sidebar... `TypeError: Cannot read properties of undefined (reading 'temperature_01')`

![one](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/7/5752c9bee5b7cf6e63b62cd1785f4aa1de2a1da7.png)

\

![Arrays](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/c/ac990765463b98eaa14e8ff31efaf96eb4a287d7.png)  
  
  
The code in the function node that sends data to my database is

```auto

let device = "IP92"; // ENTER HERE ... the IP address.
let location = "PC room"; // ENTER HERE ... Location

let new_date = new Date(); // Does NOT change with time zones across the world.
let iso = new Date().toISOString(); // RFC 3339 format ... for Grafana etc.
let timestamp = Math.round(new_date.getTime() / 1000); // getTime = milliseconds ... this removes last 3 digits to change to seconds ... for Grafana etc.

var temperature = msg.payload.data.temperature_01;
var humidity = msg.payload.data.humidity_01;
var battery = msg.payload.data.battery;

let payload =
      { 
          humidity: humidity, // ENTER HERE ... any new items to add to database, in this section.
          iso: iso,
          location: location,      
          temperature: temperature,
          timestamp: timestamp,
          battery: battery
      };

payload = JSON.stringify(payload); // Converts a JavaScript value to a JSON string

const Data_sequence = `( device,device_data) values('${device}','${payload}')` // This is the actual data.
const topic = `INSERT INTO energy ${Data_sequence}` // The insert string to send to database.

msg.topic = topic;
msg.payload = "";

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:** [5 March 2024 17:39 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/2 "2024-03-05T17:39:18Z")

</div>

So many things wrong, where to start!

> [@questuk](#):
>
> **I only want the object array with 3 values to go into my database once**.
> 
> I have an object array with 3 values
> 
> ![Object Array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/30a376b09987e8031b28d3d102de839b4e91f39a.png)

This is an object with properties. An array is something else.

> [@questuk](#):
>
> Image **No1** works but I get the same data 3 times in my database.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/5/d5b006a77151cdc1f4f2ee76d729b57e47f7c5d7.png)

You should put them in series switch (check has key) -\> switch (has other key) -\> switch (has other key) -\> link

> [@questuk](#):
>
> ```auto
> const Data_sequence = `( device,device_data) values('${device}','${payload}')` // This is the actual data.
> const topic = `INSERT INTO energy ${Data_sequence}`  
> 
> ```

This is NOT a safe way to insert data into a database. you should use prepared statements. Assuming mysql (you didnt say), the readme has info on how to do prepared queries

---

<div class="post-metadata">

**Author:** ![Buckskin](https://avatars.discourse-cdn.com/v4/letter/b/b9e5f3/32.png) [@Buckskin](https://discourse.nodered.org/u/Buckskin)\
**Post date:** [5 March 2024 17:39 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/3 "2024-03-05T17:39:26Z")

</div>

No 1 gives an output on ALL three `has key` selections because they are all valid. Just go with Option 2

No 2 Are you also sending in the `battery_voltage` `msg`, because this does not have a `temperature_01` property? If so you can (hopefully) get rid of the error message by adding a check that the data exists

```auto
if (!Object.hasOwn(msg.payload, 'data') {
  return
}

let temperature = msg.payload.data.temperature_01
let humidity = msg.payload.data.humidity_01
let battery = msg.payload.data.battery

```

I am assuming that every msg received has all three properties? If not add a check for each property in the function

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [5 March 2024 18:34 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/4 "2024-03-05T18:34:17Z")

</div>

Hi Steve and Buckskin,

Thanks for that, when I read that **Steve** , I thought I was being good for once getting the object correct 🙄  
No problem, that had been changed on my flow to read Object with properties.

I did try your suggested way and it does work, but say I want 10 values, it gets to be a long flow 🙃

> This is NOT a safe way to insert data into a database.

The output is going to a SQLite database, I will have to look at using `prepared statements`.  
But it may also go into a PostgreSQL one at some point.

**Buckskin** I also tried your way and again that worked with no errors. 😀  
and it is compact and easily expandable.

> I am assuming that every msg received has all three properties?

**Yes**

But also there is a single object with `battery_voltage` and I may be wrong, but was that causing the error ?  
  
I must admit I do not understand the

> if (!Object.hasOwn(msg.payload, 'data') {  
> return  
> }

Can you give a quick idea of what it actually does, as I always learn by my mistakes and then document it for the future 👍

I went to JSON objects thinking it would make my code easier, but it seems to have made it **much harder to understand,** if like me you don't know the correct terminology, to do a search on the internet.

Thanks so far guys for your help much appreciated, **if you or anyone else has any thoughts on improving this** please tell me, as I always thought JSON objects were more elegant to use and my code and flows looks a bit of a mess and not easy to understand.

Thanks

---

<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:** [5 March 2024 18:39 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/5 "2024-03-05T18:39:12Z")

</div>

> [@questuk](#):
>
> I went to JSON objects thinking it would make my code easier, but it seems to have made it **much harder to understand,** if like me you don't know the correct terminology, to do a search on the internet

I know & understand this fully & appreciate you are making efforts 👍

To help a little more, **JSON Object** is not _really_ a thing.

JSON is the string representation of a JavaScript Object.

It stands for JavaScript Object Notation

> [@questuk](#):
>
> I always thought JSON objects were more elegant to use and my code and flows looks a bit of a mess and not easy to understand

Using objects can be very useful and provide a means of future extensibility. Keep at it. You will "get it"

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [5 March 2024 18:53 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/6 "2024-03-05T18:53:32Z")

</div>

Hi Steve thanks,  
Your few words have clarified about the _JSON object_ that I am still using the wrong term.  
JSON is the string representation of a JavaScript Object. 👍

> [@Steve-Mcl](#):
>
> database. you should use prepared statements

Any quick pointers on this as I have looked briefly online and it does look very complicated, or in my case do I not need to as much, as the messages are coming from my own Arduino module and i wrote the code ?

As I have got older I realise i learn much quicker with visual images than with lots of text.  
Your answer with added text on my **No1 image** helped me understand easily.

Thanks

---

<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:** [5 March 2024 19:58 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/7 "2024-03-05T19:58:43Z")

</div>

> [@questuk](#):
>
> > [@Steve-Mcl](#):
> >
> > database. you should use prepared statements
> 
> Any quick pointers on this

From [the docs](https://flows.nodered.org/node/node-red-node-sqlite):

> When using Via msg.topic, parameters can be passed in the query using a msg.payload array. Ex:
> 
> ```auto
> msg.topic = `INSERT INTO user_table (name, surname) VALUES ($name, $surname)`
> msg.payload = ["John", "Smith"]
> return msg;
> 
> ```

> [@questuk](#):
>
> in my case do I not need to as much, as the messages are coming from my own Arduino module and i wrote the code

That is true yes however, if you practice best practice, you will learn best practice. Also, building these queries are often easier while also being secure by default.

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [6 March 2024 08:09 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/8 "2024-03-06T08:09:56Z")

</div>

Thanks to both of you, you gave me enough info to make changes to my Arduino code and solve the issues I had and this is now how I imagined JSON to work. 🙂

I change the battery code sent from the Arduino and added a battery ( **object**? ) with a voltage property.  
_Steve I honestly mean this, if I am using any descriptions wrong still please let me know._

So now the DHT **switch node** just looks for `data` and then the other battery **switch node** looks for `battery`

I now I have no error codes and even have data in my database !

![03](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ff56553780c195287047d4d357c2d12fa634e152.png)

![02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/aecfeca093fc142f5545ad4596f31bbf7d8b738a.png)

---

<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:** [6 March 2024 08:48 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/9 "2024-03-06T08:48:25Z")

</div>

> [@questuk](#):
>
> I change the battery code sent from the Arduino and added a battery ( **object**? ) with a voltage property.  
> _Steve I honestly mean this, if I am using any descriptions wrong still please let me know._

Perfect.

It really is worth learning the correct terms, it helps with searching and explaining issues.

Here are a few more tips I hope you find helpful:

- You can use `typeof myvar` to find the type of a thing
  - e.g. `node.warn({ typeofpayload: typeof msg.payload })`

- Objects have Properties
  - You can access them using `.` i.e: `myObject.property1`
  - You can access them using `[]` i.e: `myObject["property1"]`

- Arrays have Elements
  - The first element is 0
  - You can test how many elements it has using `myArray.length`
  - You can check it is an array using `Array.isArray(myArray)`

  

#### Objects

```auto
// below is a CONSTANT object with 3 properties
const myObj = {
   property1: 1,
   property2: "two"
   property3: [1,1,1]
}

// Constant means you cannot change the type
myObj = "hello" // ERROR!

// But you can change its properties
myObj.property1 = "one" // changed from a number 1 to a string "one"

// And you can add ...
myObj.property4 = "I am number four"

// ... and remove properties
delete myObj.property4

```

#### Arrays

```js
// create an empty array
const myArray = []

// create an empty with stuff
const myArrayWithStuff = [1, "two", [1,1,1]]

// you can add things to the end
myArrayWithStuff.push('i am number four')

// or the beginning
myArrayWithStuff.unshift('element 0')

// you can pop things off the end
let lastEl = myArrayWithStuff.pop()

// you can remove & grab the first element
let firstEl = myArrayWithStuff.shift()

// and you can clear it 
myArrayWithStuff.length = 0

// you can test a variable to see if it is an array
if(Array.isArray(myArray) {
  // yup, its an array
}

```

  

#### It is good practice to check your types

```auto
let n = "I am supposed to be a number but a bug in my flows made me a string"
if (typeof n !== 'number') {
   n = Number(n)
   if (isNaN(n)) {
     throw new Error('it is Not a Number')
   }
}

```

  
  

One last gotcha - do not rely on `typeof` without extra checks

For example:

```auto
const nObj = null
let prop1
if (typeof nObj === 'object') {
   nProp1 = nObj.property1 // ERROR: Uncaught TypeError: Cannot read properties of null (reading 'property1')
}

// leys try again
const nObj = null
let prop1
// if nObj is _something_ AND it is an object... 
if (nObj && typeof nObj === 'object') {
   nProp1 = nObj.property1
}

```

and the kicker:

```auto
const myArray = []
let element0
if (typeof myArray === 'object') {
     element0 = myArray[0] // How did we get here!!!
     // myArray is an object?
     // YES, arrays are objects. 
     // You need to use Array.isArray(myArray) to check them
}

// Lets try again
const myArray = []
let element0
// If it IS an array AND it has something in it, then you can access its elements
if (Array.isArray(myArray) && myArray.length > 0) {
    element0 = myArray[0] // Only access element if we know there is one
}

```

_👆 All untested and hand written, possible blugs lurk 😉_

---

<div class="post-metadata">

**Author:** ![smanjunath211](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/smanjunath211/32/95742_2.png) [@smanjunath211](https://discourse.nodered.org/u/smanjunath211)\
**Post date:** [6 March 2024 10:41 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/10 "2024-03-06T10:41:13Z")

</div>

Bookmarking this . Excellent Information. Thank You. 🫂

---

<div class="post-metadata">

**Author:** ![questuk](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/questuk/32/4059_2.png) [@questuk](https://discourse.nodered.org/u/questuk)\
**Post date:** [6 March 2024 10:51 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/11 "2024-03-06T10:51:44Z")

</div>

Thanks Steve a great help and a lot to read there, but I will soon, as I get a bit more time.

I am celebrating ha ha ... a few minutes ago I got **Grafana** to display Battery and Also DHT temperature in separate graphs from SQLite , still much more to do 👍

- But at least I now have, Arduino sending out **JSON string** ( I nearly typed object, but looked online to check ).

- Which via Node-RED goes to SQLite and then to Grafana 😀

**Thanks for your help and everyone else that has got me this far.**

No doubt I will be back for more advice in the not too distant future 🙃

---

<div class="post-metadata">

**Author:** ![jsprenkle](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jsprenkle/32/81249_2.png) [@jsprenkle](https://discourse.nodered.org/u/jsprenkle)\
**Post date:** [6 March 2024 13:30 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/12 "2024-03-06T13:30:18Z")

</div>

If you didn't get this fixed perhaps you might try moving the task to the stored procedure. MySql offers an "insert or update" and "insert or ignore" option. If you have a potential duplicate entry it will just discard it.

---

<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:** [4 June 2024 13:31 UTC](https://discourse.nodered.org/t/i-need-help-again-problem-with-object-array-with-3-values-into-a-function-node/86164/13 "2024-06-04T13:31:07Z")

</div>

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