简体   繁体   English

node.js在异步请求中缺少post数据

[英]node.js missing post data in async request

I'm making a simple form in Node.js. 我在Node.js中创建一个简单的表单。 Everything else seems to be working correctly, but the function that is supposed to receive post request data is never getting called. 其他一切似乎都正常工作,但是应该接收post请求数据的函数永远不会被调用。 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. 函数onRequestEnd会被调用,但是稍后我的代码会在参数体中只有一个空字符串时中断。 Is the keyword 'data' correct? 关键字“数据”是否正确?

The code was modified from an answer here: How do you extract POST data in Node.js? 代码是从这里的答案修改的: 如何在Node.js中提取POST数据? . 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 这就是我的感受: 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. request.data是这里需要注意的重要事项。 However I haven't really solved how to "not" have the request.on('end'...) in my view yet. 但是我还没有真正解决过如何在我的视图中“不”拥有request.on('end'...)

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. 关于为什么console.log()将如何处理来自您完成所有这些工作的函数的回调的原因。

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 最后是我执行的preRequest()函数

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); 并添加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(); 另外,这对我来说可能是非常愚蠢的,但是你知道那个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. 这可能不是你的确切答案,但我希望这有点帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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