Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/61.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql node.js:数据库查询+;多部分表单数据锁定_Mysql_Node.js_Express_Multipart - Fatal编程技术网

Mysql node.js:数据库查询+;多部分表单数据锁定

Mysql node.js:数据库查询+;多部分表单数据锁定,mysql,node.js,express,multipart,Mysql,Node.js,Express,Multipart,我对整个node还是相当陌生的,所以如果这是非常愚蠢的,请原谅我,但是: 如果我在节点mysql客户端对象上调用query(),我将无法再从我的web应用程序(express)中获取请求对象的主体 我的基本POST/PUT模式请求需要用户身份验证: _db_client = connect_to_db_server(); app.post("/some/path", function (req, res) { _db_client.query("SELECT * FROM Users W

我对整个node还是相当陌生的,所以如果这是非常愚蠢的,请原谅我,但是:

如果我在节点mysql客户端对象上调用
query()
,我将无法再从我的web应用程序(express)中获取请求对象的主体

我的基本POST/PUT模式请求需要用户身份验证:

_db_client = connect_to_db_server();
app.post("/some/path", function (req, res) {
    _db_client.query("SELECT * FROM Users WHERE username = ?",
                     [ http_basic_auth.username ], function (err, results) {
      if (err != null || resp == null) {
         res.send("AUTH FAILURE: sorry, you can't do that", 403);
      } else {
         get_request_body(req, function (body) {
            do_something();
            res.send("yay, I'm happy", 200);
         });
      }
   });
});
现在,问题是get_request_body函数永远不会回来。它似乎只是在请求对象上被“阻止”

以下是一个示例程序:

var express = require('express'),
    mysql = require('mysql');

var MySQLClient = mysql.Client;
var client = new MySQLClient();

client.user = 'root';
client.password = '';

client.connect(function(err, res) {
    client.query("USE Gazelle", function (qerr, qresp) {
    });
});

var app = express.createServer(  );
app.use(express.bodyParser());

app.post("/test", function (req, res) {
    client.query('SELECT * FROM Users', function (err, results) {
        get_request_body(req, function (body) {
            console.log("wooo");
            console.log(body);
            res.send("cool", 200);
        });
    });
});


app.listen(8080);


function get_request_body (req, callback) {
    if (req.rawBody != undefined) {
        callback(req.rawBody);
    } else {
        var data = '';
        req.setEncoding('binary');

        req.on('data', function(chunk) {
            data += chunk;
        });

        req.on('end', function() {
            callback(data);
        });
    }
}
诀窍:仅当我使用多部分表单POST数据而不是常规POST数据时,它才会失败:

curl -u un:pw -X POST -d ' { "cat" : "meow" }' http://localhost:8080/test
工作

锁起来,再也回不来了。我在connectform和multipartjs中看到了相同的错误。现在有点难倒了


我是个十足的白痴吗?这不是正确的处理方式吗?

我认为当您将
end
侦听器添加到请求对象时,
end
事件已经触发。您需要将IO事件侦听器立即添加到主
app.post
函数范围中的
req
对象中,而不是在DB查询返回时的异步回调中。这是一个包含一些日志语句的版本,这些语句显示了导致此问题的事件序列。
req.emit('end')只是一个黑客程序,用来证明请求被锁定的原因。您的
end
事件处理程序从未被调用,但合成重复的
end
事件确实允许完成请求(但未正确解析正文)

curl -u un:pw -X POST -F "image_file=@test.jpg" http://localhost:8080/test
var express = require('express');
var app = express.createServer(  );
app.use(express.bodyParser());

app.post("/test", function (req, res) {
  req.on('data', function(){
    console.log("data fired but your listener is not yet added");
  });
  req.on('end', function() {
    console.log('end fired but your listener is not yet added');
  });
  setTimeout(function() { //Simulating your async DB query
    console.log("setTimeout finished and fired callback");
        get_request_body(req, function (body) {
            console.log("wooo");
            console.log(body);
            res.send("cool", 200);
        });
      }, 50);
});


app.listen(8081);


function get_request_body (req, callback) {
    if (req.rawBody != undefined) {
        console.log('express.bodyParser parsed it already');
        callback(req.rawBody);
    } else {
        console.log('get_request_body going to parse because ' + req.rawBody);
        var data = '';
        req.setEncoding('binary');

        req.on('data', function(chunk) {
            console.log('get_request_body got data event');
            data += chunk;
        });

        req.on('end', function() {
            console.log('get_request_body parsed it');
            callback(data);
        });
        req.emit('end');//BUGBUG
    }
}