# TypeError: Cannot convert undefined or null to object

**URL:** <https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264>\
**Category:** General\
**Created:** [15 June 2019 12:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264 "2019-06-15T12:52:47Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [15 June 2019 12:52 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/1 "2019-06-15T12:52:47Z")

</div>

My data is a json format. However, there are some null in my data. How can I fix the problem. Can I change null to string. How can I change by using java?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 June 2019 13:14 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/2 "2019-06-15T13:14:50Z")

</div>

You will need to give us much more to go on than this. We have no idea what your data looks like.

Guessing from the title, I'd say that you have misunderstood or mis-written a property path such that it doesn't actually exist. So the question of how to change null to a string is not relevant.

Also, the query about Java is also not relevant - Node-RED uses JavaScript which is an entirely different language.

---

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [15 June 2019 13:30 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/3 "2019-06-15T13:30:57Z")

</div>

@TotallyInformation Thanks for your reply.  
This is my data. I would want to process insert query. However, it appears TypeError when I debug.

> var keyName = Object.keys(msg.data)[msg.start] ;  
> var Changetime = msg.data[keyName].Changetime;  
> var Class3 = msg.data[keyName].Class3;  
> var Class2 = msg.data[keyName].Class2;  
> var Class1 = msg.data[keyName].Class1;  
> var Py = msg.data[keyName].Py;  
> var Px = msg.data[keyName].Px;  
> var Gov = msg.data[keyName].Gov;  
> var Picdescribe3 = msg.data[keyName].Picdescribe3;  
> var Picture3 = msg.data[keyName].Picture3;  
> var Picdescribe2 = msg.data[keyName].Picdescribe2;  
> var Picture2 = msg.data[keyName].Picture2;  
> var Picdescribe1 = msg.data[keyName].Picdescribe1;  
> var Picture1 = msg.data[keyName].Picture1;  
> var Opentime = msg.data[keyName].Opentime;  
> var Zipcode = msg.data[keyName].Zipcode;  
> var Add = msg.data[keyName].Add;  
> var Tel = msg.data[keyName].Tel;  
> var Description = msg.data[keyName].Description;  
> var Toldescribe = msg.data[keyName].Toldescribe;  
> var Name = msg.data[keyName].Name;  
> var Id = msg.data[keyName].Id;  
> msg.topic = "INSERT INTO travel (Changetime, Class3, Class2, Class1, Py, "+  
> "Px, Gov, Picdescribe3, Picture3, Picdescribe2, Picture2, Picdescribe1, Picture1, Opentime, Zipcode, Add, Tel, Description, Toldescribe, Name, Id) VALUES ('" + Changetime + "', '" + Class3 + "', '" + Class2 + "', '" + Class1 +  
> "', '" + Py + "', '" + Px + "', '" + Gov + "', '" + Picdescribe3 + "', '" + Picture3 + "', '" + Picdescribe2 + "', '" + Picture2 + "', '" + Picdescribe1 + "', '" + Picture1 + "', '" + Opentime + "', '" + Zipcode + "', '" + Add + "', '" + Tel + "', '" + Description + "', '" + Toldescribe + "', '" + Name + "', '" + Id + "')";
> 
> msg.start = msg.start + 1;  
> return msg;

This is one of my Json data.

> [{"Changetime":"2017/07/05 13:15:08","Class3":"","Class2":"","Class1":"10","Py":"25.22186","Px":"121.64589","Gov":"382000000A","Picdescribe3":"123","Picture3":"[http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115909.jpg","Picdescribe2":"234","Picture2":"http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115902.jpg","Picdescribe1":"345","Picture1":"http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115854.jpg","Opentime":"09：00-24：00","Zipcode":"208","Add":"5345","Tel":"886-2-24082628","Description":"a](http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115909.jpg%22,%22Picdescribe2%22:%22234%22,%22Picture2%22:%22http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115902.jpg%22,%22Picdescribe1%22:%22345%22,%22Picture1%22:%22http://tour.ntpc.gov.tw/Content/Upload/Place/20150709115854.jpg%22,%22Opentime%22:%2209%EF%BC%9A00-24%EF%BC%9A00%22,%22Zipcode%22:%22208%22,%22Add%22:%225345%22,%22Tel%22:%22886-2-24082628%22,%22Description%22:%22a) good place \r\n\r\n the place is awesome","Toldescribe":"It has been ten years","Name":"new york","Id":"C1\_382000000A\_102485"}]

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 June 2019 13:34 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/4 "2019-06-15T13:34:34Z")

</div>

When posting, please wrap code with back-tags so that it is readable.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 June 2019 13:36 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/5 "2019-06-15T13:36:33Z")

</div>

You will need to add a debug node before this function so that you can see what the correct layout is for the msg you are passing in.

I rather suspect that where you have used `msg.data` you might have meant `msg.payload.data`? But I can't tell.

---

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [15 June 2019 13:44 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/6 "2019-06-15T13:44:13Z")

</div>

I see. I would keep in mind.

---

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [15 June 2019 13:48 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/7 "2019-06-15T13:48:44Z")

</div>

Yes, **msg.data** meant **`msg.payload.data`**. This is the data before process insert query. However, it is still error.

> var obj = msg.payload;  
> if (obj.retCode == "1")  
> {  
> msg.repetitions = Object.keys(obj.retVal).length;  
> msg.data = obj.retval;  
> msg.start = 0;  
> }
> 
> return msg;

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [15 June 2019 14:55 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/8 "2019-06-15T14:55:50Z")

</div>

What is the exact full error you are getting?

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [15 June 2019 17:59 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/9 "2019-06-15T17:59:34Z")

</div>

That doesn't seem to match the data you gave earlier.

---

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [16 June 2019 04:20 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/10 "2019-06-16T04:20:41Z")

</div>

@TotallyInformation Thanks for your help. I had fixed it. Nonetheless, I guess that there are some problem in my database ,can you help me find the problem?

> ```
> name type
> 
> ```
> 
> 1 Changetime &nbsp; &nbsp; &nbsp; &nbsp; `datetime`  
> 2 Class3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(2)`  
> 3 Class2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(2)`  
> 4 Class1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; `varchar(2)`  
> 5 Py &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ` varchar(20)`  
> 6 Px &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(20)`  
> 7 Gov &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(20)`  
> 8 Picdescribe3 &nbsp; &nbsp; &nbsp; &nbsp; ` text`  
> 9 Picture3 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(200)`  
> 10 Picdescribe2 &nbsp; &nbsp; &nbsp; `varchar(50)`  
> 11 Picture2 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(200)`  
> 12 Picdescribe1 &nbsp; &nbsp; &nbsp; `text`  
> 13 Picture1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(200)`  
> 14 Opentime &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(60)`  
> 15 Zipcode &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(10)`  
> 16 Add1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ` varchar(40)`  
> 17 Tel &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(40)`  
> 18 Description &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ` text`  
> 19 Toldescribe &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(500)`  
> 20 Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(30)`  
> 21 Id &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `varchar(50)`

And there is a new error.

> "Error: ER\_PARSE\_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Add, Tel, Description, Toldescribe, Name, Id) VALUES ('2017/09/11 12:43:21', '',' at line 1"

---

<div class="post-metadata">

**Author:** ![charlie32305](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@charlie32305](https://discourse.nodered.org/u/charlie32305)\
**Post date:** [16 June 2019 04:41 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/11 "2019-06-16T04:41:47Z")

</div>

There is a new error I get.

> "Error: ER\_PARSE\_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'Add, Tel, Description, Toldescribe, Name, Id) VALUES ('2017/09/11 12:43:21', '',' at line 1"

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [16 June 2019 06:21 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/12 "2019-06-16T06:21:21Z")

</div>

It would help if you wired a debug node next to whatever node you have trying to perform that query so we can see the full SQL query you are trying to perform. It is then a matter of spotting the syntax error.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [16 June 2019 07:55 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/13 "2019-06-16T07:55:03Z")

</div>

I notice that you are trying to write to a column called Add, but there is no such column in the database, though that would not give a syntax error.

---

<div class="post-metadata">

**Author:** ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)\
**Post date:** [16 June 2019 10:10 UTC](https://discourse.nodered.org/t/typeerror-cannot-convert-undefined-or-null-to-object/12264/14 "2019-06-16T10:10:58Z")

</div>

> [@charlie32305](#):
>
> near 'Add

You have "Add1" in your table definition.
