NodeJs Sql问题

时间:2016-10-03 21:46:22

标签: javascript mysql sql-server node.js callback

我正在使用NodeJs回调函数并在其中执行2个sql查询。但得到了意想不到的结果。我相信它是一个回调问题,但无法弄明白。

exports.addProducts = function(req, res) {
  // check user already exists
  var B_Usrname = req.session.username;
  var id = req.param('item_id');
  var Qty = req.param('Qty');
  var json_responses;
  var getDetails = "select * from product where ITEM_CODE='" + id + "'";
  var addUser = "INSERT INTO cart (ITEM_CODE, ITEM_NAME, ITEM_DESC, QUANTITY, SELLER_USERNAME, BUYER_USERNAME, PRICE) VALUES" +
    "('" + id + "','" + name + "','" + desc + "','" + Qty + "','" + S_Usrname + "','" + B_Usrname + "','" + Price + "');";

  console.log("Query is:" + getDetails);
  console.log("Query is:" + addUser);
  mysql.fetchData(function(err, results) {
    if (err) {
      throw err;
    } else {
      if (results.length > 0) {
        console.log("Data Fetched Successfully." + "\n");
        console.log(results);
        for (var i in results) {
          var output = results[i];
          var name = output.ITEM_NAME;
          var desc = output.ITEM_DESC;
          var Qty1 = output.ITEM_QTY;
          var S_Usrname = output.SELLER_USERNAME;
          var Price = output.Price;
        }
        //sql query to insert the values in the  cart
        mysql.fetchData(function(err, results) {
          if (err) {
            throw err;
          } else {
            console.log("Data Inserted Successfully." + "\n");
            // render on success
          }
        }, addUser);
      }
    }
  }, getDetails);
};

我得到的控制台上的输出是:

SQL Query::select * from product where ITEM_CODE='3'
Mon, 03 Oct 2016 20:48:15 GMT express deprecated req.param(name):      Use     req.params, req.body, or req.query instead at ../routes    /cart.js:123:16

Connection closed....
Database Results:[object Object]
Data Fetched Successfully.

[ RowDataPacket {
    ITEM_CODE: 3,
    ITEM_NAME: 'SONY',
    ITEM_DESC: ' 44 inches',
    ITEM_PRICE: 170,
    ITEM_QTY: 4,
    SELLER_FIRSTNAME: 'sagar',
    SELLER_LASTNAME: 'kaw',
    EMAIL: 'sagar.kaw@gmail.com',
    SELLER_USERNAME: 'sagkaw',
    category: 'TV',
    Group_Name: 'Electronics' } ]

SQL Query::INSERT INTO cart (ITEM_CODE, ITEM_NAME, ITEM_DESC,     QUANTITY, SELLER_USERNAME, BUYER_USERNAME, PRICE)     VALUES('3','undefined','undefined','2','undefined','sahraw','undefined');

Connection closed....

我不确定为什么在插入查询中使用undefined以及如何解析它。我必须将从Select查询中提取的值传递给Insert Query。

0 个答案:

没有答案