简体   繁体   中英

node.js missing post data in async request

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM