# Sql to chart coding error

**URL:** <https://discourse.nodered.org/t/sql-to-chart-coding-error/24121>\
**Category:** Dashboard\
**Created:** [2 April 2020 23:40 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121 "2020-04-02T23:40:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [2 April 2020 23:40 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121/1 "2020-04-02T23:40:47Z")

</div>

I'm using a sql statement `msg.topic='SELECT HOUR(Time) as Htime, Temperature FROM `temp-at-interrupt` where minute(Time)= "00" order by Date DESC, Time DESC LIMIT 20';`  
which returns the following structure: array[20]  
[0 … 9]  
0: object  
Htime: 19  
Temperature: 83.33  
1: object  
Htime: 18  
Temperature: 83.09  
2: object  
Htime: 17  
Temperature: 82.79  
The function node is:

```auto
 // Create a data variable   
 //  
 var series = ["temp F"];  
 var labels = ["Data Values"];  
 var data = "[[";  
   
 for (var i=0; i < msg.payload.length; i++) {  
     //var myHour = msg.payload[i].Htime;
   data += '{ "x":' + msg.payload[i].Htime + ', "y":' + msg.payload[i].Temparature + '}';  
   //data += '{ "x":' + msg.payload[i].thetime + ', "y":' + msg.payload[i].thetemp + '}';  
   if (i < (msg.payload.length - 1)) {  
     data += ","  
   } else {  
     data += "]]"  
   }  
 }  
 var jsondata = JSON.parse(data);  
 msg.payload = [{"series": series, "data": jsondata, "labels": labels}];  
   
return msg;

```

The debug errors are:  
"  
4/2/2020, 7:21:45 PM[node: sql-lite-function](http://X.Y.Z.124:1882/#)function : (error)  
"SyntaxError: Unexpected token u in JSON at position 16"  
"

Seems pretty easy by I'm missing something. I've been working on this all week and need your expertise. Appreciate your expertise as I'm really, really new.  
I wish I has a line-by-line error checking app instead of a debug-node. There's just not enough information for me to troubleshoot.

---

<div class="post-metadata">

**Author:** ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)\
**Post date:** [2 April 2020 23:47 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121/2 "2020-04-02T23:47:38Z")

</div>

Try use in `node.warn(“x=“+x)` in your function

---

<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:** [3 April 2020 07:45 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121/3 "2020-04-03T07:45:08Z")

</div>

Also you appear to be trying (and failing) to create a JSON string representing the object you want and then converting it to an object. It is generally much easier just to build the javascript object directly, then you don't have all that messing about with strings which, as you have found out, is very easy to get wrong.

---

<div class="post-metadata">

**Author:** ![chuckf201](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/chuckf201/32/91382_2.png) [@chuckf201](https://discourse.nodered.org/u/chuckf201)\
**Post date:** [3 April 2020 13:37 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121/4 "2020-04-03T13:37:42Z")

</div>

Zenofmud: Thanks for the suggestion. node.warn and node.error did help.  
Collin: Advice taken I will rewrite the function.  
Thanks to you both.

---

<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:** [3 May 2020 13:37 UTC](https://discourse.nodered.org/t/sql-to-chart-coding-error/24121/5 "2020-05-03T13:37:43Z")

</div>

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