Impossible to parse simple array from SQL node

I have tried so many different ways, but as far as I understand all documentation, this should work. But it only passes "", and not the value 152 which is returned by the SQL query. This is the query I use (to get the current energy price from my price table):

SELECT TOP 1 cast(TotalPrice as numeric(8,0)) AS TotalPrice FROM insertTableName ORDER BY Date DESC, Hour DESC

I'm using v3.0.2 (Unraid, docker container) - Module: node-red-contrib-mssql-plus

I hate mustache, it never does what I need and it's yet another language to learn. Try a change node:

[{"id":"244064707c409717","type":"inject","z":"451d9acba8e52c2f","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"TotalPrice\": 152}]","payloadType":"json","x":590,"y":140,"wires":[["ce2fbc88595972ee","d6fd584b499aae35"]]},{"id":"ce2fbc88595972ee","type":"debug","z":"451d9acba8e52c2f","name":"debug 21","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":800,"y":200,"wires":[]},{"id":"d6fd584b499aae35","type":"change","z":"451d9acba8e52c2f","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload[0].TotalPrice","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":800,"y":120,"wires":[["ce2fbc88595972ee"]]}]

Mustache is really simple (this was the idea), especially for someone who is dealing with vue, this should be mickey mouse stuff :wink:

Then again, it will be confusing when accessing array elements directly by index within mustache, the formatting will be:

payload.0.TotalPrice
1 Like

LOL!
I actually looked through the docs and searched for array. No hit. That's why I don't use that stuff: if I pull up a function node I don't need to look up any docs, I just write a 1-liner, and as a bonus I can even add a comment... :nerd_face:

1 Like

Thank you. I ended up using script instead figuring out the template node is not that good.

const object = msg.payload[0];
const TotalPrice = object.TotalPrice;
const formattedTotalPrice = TotalPrice.toFixed(2);
const result = {
CurrentTemperature: formattedTotalPrice
};
msg.payload = result;
return msg;

Edit: I'm passing the price as a temperature service for Apple Homekit to control stuff at home

1 Like

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