使用nodejs和mysql

时间:2015-10-19 09:50:18

标签: javascript mysql node.js express

我是NodeJS的新手,我正在尝试创建一个api服务,我已经创建了get函数,但它的工作正常但是post函数没有:

router.post("/venditori",function(req,res){
        var query = "INSERT INTO ??(??,??,??,??,??) VALUES (?,?,?,?,?)";
        var table = ["venditori","name","surname","company","email","password",req.body.name,req.body.surname,req.body.company,req.body.email,md5(req.body.password)];
        query = mysql.format(query,table);
        connection.query(query,function(err,rows){
            if(err) {
                res.json({"Error" : true, "Message" : "Error executing MySQL query"});
            } else {
                res.json({"Error" : false, "Message" : "User Added !"});
            }
        });
    });

并告诉我这个错误:

TypeError: Object function (size) {  if ('number' == typeof size) return convert(size);  var parts = size.match(/^(\d+(?:\.\d+)?)
*(kb|mb|gb|tb)$/)    , n = parseFloat(parts[1])    , type = parts[2];

 var map = {
     kb: 1 << 10    , mb: 1 << 20    , gb: 1 << 30    , tb: ((1 << 30) * 1024)  };

 return map[type] * n; } has no method 'parse'
      at getRawBody (/var/www/vhosts/example.com/httpdocs/node_modules/raw-body/index.js:86:21)
      at read (/var/www/vhosts/example.com/httpdocs/node_modules/body-parser/lib/read.js:64:3)
      at urlencodedParser (/var/www/vhosts/example.com/httpdocs/node_modules/body-parser/lib/types/urlencoded.js:104:5)
      at Layer.handle [as handle_request] (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/layer.js:82:5)
      at trim_prefix (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/index.js:302:13)
      at /var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/index.js:270:7
      at Function.proto.process_params (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/index.js:321:12)
      at next (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/index.js:261:10)
      at expressInit (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/middleware/init.js:23:5)
      at Layer.handle [as handle_request] (/var/www/vhosts/example.com/httpdocs/node_modules/express/lib/router/layer.js:82:5)

3 个答案:

答案 0 :(得分:0)

console.log(req,res);之前尝试var query = ...并将其粘贴到此处。看起来这两个变量没有序列化为JSON,当/users没有返回预期值时,可能会发生这种情况。

答案 1 :(得分:0)

我已修复删除node_modules目录并使用另一个package.json运行新的npm安装。查询正常,问题与body-parser和jwt-simple的版本相关

答案 2 :(得分:0)

请注意&#39;错误&#39;来自MySQL是一个阻止正确执行的实际错误,例如语法错误。一个&#39;错误&#39;来自INSERT语句实际上可能是成功执行,但操作失败;例如一个独特的密钥违规等。

在您的代码中,您需要查看&#39; result.affectedRows&#39;确定查询是否正确执行但语句失败。值为&#39; 0&#39;表示INSERT语句正确执行但无法插入行,无论出于何种原因。价值为&#39; 1&#39;意味着成功。像这样:

    connection.query(query,function(err,result) {
        if(err) {
            // this is a SQL execution error
            res.json({"Error" : true, "Message" : "Error executing MySQL query"});
        } else { // these are execution 'successes' which include failures
            if(result.affectedRows == 1) { // succesful INSERT
                res.json({"Error" : false, "Message" : "User Added !"});
            } else if(result.affectedRows == 0) { // failed INSERT
                res.json({"Error" : true, "Message" : "INSERT failed !"});
            }
        }
    });

有关详情,请访问:https://github.com/felixge/node-mysql#getting-the-number-of-affected-rows