post call在节点js中不起作用

时间:2016-09-13 05:02:03

标签: mysql node.js express

我正在尝试在节点js中执行post调用,我正在通过post测试它,但我无法检索数据, 我的节点代码,

 exports.login = function( req, res ) {
 console.log("Params:"+req.body.email);
  //console.log('email:'+params.email);
 connection.query('SELECT * FROM profile where email ='+req.body.email,function(error,result,rows,fields){
    if(!!error){console.log(error)
      console.log('fail');
    }else{
      console.log(result);
      res.send(result);
    }
  // }

  });}

我的路线,

 router.post('/login',cors(), admin.login);

我失败了,我的错误是

{ [Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gmail.com' at line 1]

我通过邮递员输入

{"email":"s@gmail.com"}

1 个答案:

答案 0 :(得分:1)

不要直接构建查询字符串,这会使您对注入攻击以及某些字符的阻塞开放,就像您在这里遇到的那样。像这样使用占位符:

var query = "select * from profile where email = ?";

connection.query(query, [req.body.email], function(error,result,rows,fields) {
...