4

I'm making a simple form in Node.js. Everything else seems to be working correctly, but the function that is supposed to receive post request data is never getting called. Here's the relevant code snippet:

if (request.method == 'POST') {
    var body = '';
    console.log(request.body);
    request.on('data', function (chunk) {
        console.log("got the post request data"); //nothing logged to console
        body += chunk;
    });
    request.on('end', onRequestEnd(body, response));
}

The function onRequestEnd does get called, but later my code breaks when there's nothing but an empty string in the parameter body. Is the keyword 'data' correct?

The code was modified from an answer here: How do you extract POST data in Node.js?. I'll post more if needed.

Community
  • 1
  • 1
emllnd
  • 111
  • 1
  • 9
  • Glad to see another swedish node.js dev. Anyways you're calling it correctly. What does your onRequestEnd() return? What I found that I had to do was to make another request.on('end', function(err){}); in my "view". – Henrik Andersson Nov 16 '12 at 10:12
  • Actually I'm Finnish. My name's totally Swedish though! – emllnd Nov 19 '12 at 19:09

2 Answers2

1

After lots of frustration I solved the problem myself!

I changed the line:

request.on('end', onRequestEnd(body, response));

to:

request.on('end', function() {
        onRequestEnd(body, response);
    });

It had something to do with callbacks. I'm not exactly sure why this works and the other one doesn't though. This is how I feel: http://www.masti-xpress.com/images/Story-of-Every-Programmer.jpg

emllnd
  • 111
  • 1
  • 9
  • Does the onRequestEnd() return something? – Henrik Andersson Nov 19 '12 at 20:38
  • 2
    The reason why the first doesn't work is because you are not passing a function to request.on() to call when the request ends. Rather you are calling the function! (and passing its results to request.on()). A function name without args, or an anonymous function definition, is a reference to the function. A function with args as in your first case is an *invocation* of the function. Also, if you are using Express's bodyParser middleware to get request.body, you do not need to assemble chunks using an on(data) callback. – ravi May 28 '13 at 15:56
0

I'll share how I solved the problem with this. I had another view of it however and I'll share that as well. What I wanted was to have something like this in my "view".

app('/urlToView', function(req, response){
    request.on('end', function() {
        var post = **request.data;** //sanitize data
        resolver.renderTemplateOr404('start.html', post, request, response);
    });
}

The request.data is the important thing to notice here. However I haven't really solved how to "not" have the request.on('end'...) in my view yet.

A reason as to why the console.log() would be how you handle the callback from the function that you do all this work in.

I hijack the request before it lands in my view when I start the server

self.preProcess(self, request, response);

and

preProcess: function onRequest(app, request, response){ 
     processor.preRequest(request);
}

and lastly int the preRequest() function I do

if (request.method === 'POST') {
    var postdata = "";
    request.on('data', function(postdataChunk){
         postdata += postdataChunk;
    });
    request.on('end', function(){
        _transformRequest(request, _transformPostdata(postdata)); //this is to set the data on the request
    });
}

and adding a console.log(postdataChunk); here isn't a problem since all of the callbacks are properly handled.

Also, this might be very stupid of me to ask but are you aware of that console.log(); doesnt output to browser but to the terminal window?

This might not be an exact answer for you but I hope this helps a bit.

Henrik Andersson
  • 45,354
  • 16
  • 98
  • 92
  • Thanks for your answer! It wasn't quite what I was looking for though. I do know that console.log(); outputs to the terminal but I can totally see how you could think otherwise based on my question. Actually that line was left there by accident! – emllnd Nov 19 '12 at 19:16