Search code examples
javascriptjsonnode.jsphilips-hue

Having problems with parsing post data in node


This is really bugging me. I have a node server that receives post data, parses it, and then sends it back out as part of a json string. But for some reason it's not working.

The incoming post data is formatted: rgb=rgb(xxx%2Cxxx%2Cxxx)

I parse it and remove the rgb and parentheses:

var str = (querystring.parse(postData).rgb);
var str=S(str).replaceAll('%2C', ',').s;
var str=S(str).replaceAll('rgb(', '').s;
var rgb=S(str).replaceAll(')', '').s;

That leaves me with: xxx,xxx,xxx

Which is exactly what I need to send out to this json string:

hue.light(Light1, function(light){
hue.change(light.set({'rgb':[rgb]}));
 });

But it doesn't work.

This works, so I know I'm on the right track:

hue.light(Light1, function(light){
hue.change(light.set({'rgb':[xxx,xxx,xxx]}));
 });

Is there some type of conversion I need to make to the string to make it work?


Solution

  • rgb is a string containing comma-separated values. You need to parse these values and put them in an array. Try this:

    rgb = rgb.split(',')
             .map(function(str) { return parseInt(str, 10); });