如何使用nodejs

时间:2019-05-22 07:11:21

标签: node.js sql-server express body-parser

我正在尝试使用带有nodejs的窗口身份验证将查询插入SQL Server。我已经完成了选择查询的获取请求。但现在我尝试使用插入查询来发布请求。但是我无法将我的req.body.name传递给以下查询

app.post('/ping', bodyparser.json(), function(req, res) {
    console.log("post enter");

    const query = "insert into student (name) values ("+req.body.name+") ";

    sql.query(connectionString, query, (err,rows) => {
        console.log(rows);
        if (err) {
            console.log(err)
        }
        else {
            return  res.send(rows);
        }
    })
});

1 个答案:

答案 0 :(得分:0)

尝试这样:

    app.post('/register',function(req,res){
          var users={
    "first_name":req.body.first_name,
    "last_name":req.body.last_name,
    "email":req.body.email,
    "password":req.body.password,
    "created":today,
    "modified":today
  }
  connection.query('INSERT INTO users SET ?',users, function (error, results, fields) {
  if (error) {
    console.log("error ocurred",error);
    res.send({
      "code":400,
      "failed":"error ocurred"
    })
  }else{
    console.log('The solution is: ', results);
    res.send({
      "code":200,
      "success":"user registered sucessfully"
        });
  }
        });
    });