mysql - 后调用在 Node js 中不起作用

标签 mysql node.js express

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

 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]

我通过 postman 输入

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

最佳答案

不要直接构建查询字符串,这会让您容易受到注入(inject)攻击,并且还会阻塞某些字符,正如您在这里所经历的那样。使用如下占位符:

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

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

关于mysql - 后调用在 Node js 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39462488/

相关文章:

node.js - 如何在 Sequelize upsert 中获取插入或更新记录的 ID?

node.js - 单个路由的 gzip 响应

javascript - SuperTest - 应用程序未定义

mysql - 触发器在 PhpMyAdmin sql 选项卡中不起作用

php - 使用 SQL 查询单个值

php - 为什么这个存储过程不起作用?

php - phpmyadmin 中的 ID

javascript - 如何维护对正确对象的引用?

node.js - 为什么我在查询用户项时会出现 401 错误?

node.js - Nodejs 应用程序在本地运行,但在部署时无法运行