Sql to chart coding error

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:

 // 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 PMnode: sql-lite-functionfunction : (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.

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

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.

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

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