0

Possible Duplicate:
JSON array in Node.js

Im kinda new to node.js and Im working on only the server side to answer POST from the client side. What i need to do is when a POST request is made at location '/sort' with parameter 'theArray', sort the array removing all non-string values and return the resulting value as JSON. theArray parameter will be a stringified JSON Array. I have tried this code here:

case '/sort':
        if (req.method == 'POST') {
            res.writeHead(200,{
                'Content-Type': 'application/json'
            });
            var fullArr = "";
                req.on('data', function(chunk) {
                    fullArr += chunk;
                    });
                req.on('end', function() {
                            var query = JSON.parse(fullArr);
                            var arr = "";
                            var par = query.theArray;
                            arr += par;
                    console.log(arr); 

                                function censor(key, value) {
                                    if (typeof value == "string") {
                                            return value;
                                        } 
                                        return undefined;
                                        }
                        var jsonString = JSON.stringify(arr, censor);
                        console.log(jsonString);
                });         
                    res.end();


        };

break;

But it just returns the same thing? I have also tried replacing the qs.parse with the JSON.parse and it just returned undefined? Can someone please help! Thanks

2
  • So the difference between using qs or JSON for parse depends on you, what do you send to this POST ? a Body urlencoded or a JSON string ? Commented Aug 8, 2012 at 6:15
  • I guess i should have been a little but more specific about this. But i dont have control over what is sent. this is kinda like an assignment. I think it is urlencoded though because when messing with some code ive gotten back an error and the console.log showed undefined and some code that looked to be encoded (%2b%3...). But either parse doesnt tack out the non-string values? Commented Aug 8, 2012 at 6:26

2 Answers 2

0

Check this:

  1. Request data received by chunks. There is absolutely no guarantees that you receive all data in first chunk. You need to parse input data only after 'end' event fired.
  2. In censor function you have undefined variable 'i'. Do you mean 'key'?
Sign up to request clarification or add additional context in comments.

6 Comments

thanks i have edited the code above. but i think that i would be looking for the value because the parameter given would be something like: {"theArray":"[[],\"d\",\"B\",{},\"b\",12,\"A\",\"c\"]"} But i still get the same results
God I am probably just over thinking this and after 4 days of trying every code that i come across I am probably confusing myself by now
If fullArr is {"theArray":"[[],\"d\",\"B\",{},\"b\",12,\"A\",\"c\"]"} qs.parse do not parse it. qs.parse designed for something like this: a=1&b=2&c=3. Try JSON.parse.
Okay well then if i just try and respond with the fullArr it responds it coded like this "theArray=%5B%5B%5D%2C%22d%22%..'
i have also tried the JSON.parse and it give a result of undefined:1 and then gives an error starting with the code result like this %2C%22A%22%2C%22 SyntaxError: Unexpected token h
|
0

wait till the request has ended.. then parse your fullArray

var data = ""
req.on('data',function(chunk){data+=chunk})
req.on('end',function(){...parse here....})

1 Comment

thanks i edited the code above but still get the same results

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.