2017-09-08 90 views
0

我想通過傳遞一個令牌號碼來搜索一個mysql數據庫,並且我想從數據庫中返回與該令牌相關的id。 我已經嘗試過,但我不能看到任何結果集,我無能爲力,因爲我做錯了什麼。 這是我到目前爲止所嘗試的。沒有結果集返回where子句節點js mysql?

checkifuniquenumberexists:function(num){ 
     con.connect(function (err) { 
      if (err) throw err; 
      console.log("Connected!"); 
      var post = {"token": num}; 
      var query = con.query('select id from uniquereviewtoken where token in (?)', post, function (err, result,fields) { 
       // Neat! 
      //console.log(query); 
       console.log("error", err); 
       console.log("result", result.length); 
       console.log("fields",fields); 
       return result; 


      }); 

     }); 

mysql語法有什麼問題嗎?

這是我在我的日誌

1|service | undefined 
1|service | Connected! 
1|service | error null 
1|service | result 0 
1|service | fields [ FieldPacket { 
1|service |  catalog: 'def', 
1|service |  db: 'review', 
1|service |  table: 'uniquereviewtoken', 
1|service |  orgTable: 'uniquereviewtoken', 
1|service |  name: 'id', 
1|service |  orgName: 'id', 
1|service |  charsetNr: 63, 
1|service |  length: 50, 
1|service |  type: 3, 
1|service |  flags: 16899, 
1|service |  decimals: 0, 
1|service |  default: undefined, 
1|service |  zeroFill: false, 
1|service |  protocol41: true }, 
1|service | FieldPacket { 
1|service |  catalog: 'def', 
1|service |  db: 'review', 
1|service |  table: 'uniquereviewtoken', 
1|service |  orgTable: 'uniquereviewtoken', 
1|service |  name: 'token', 
1|service |  orgName: 'token', 
1|service |  charsetNr: 33, 
1|service |  length: 3000, 
1|service |  type: 253, 
1|service |  flags: 4097, 
1|service |  decimals: 0, 
1|service |  default: undefined, 
1|service |  zeroFill: false, 
1|service |  protocol41: true } ] 

回答

1

我得到你不需要post對象。 Ref:https://www.npmjs.com/package/mysql#preparing-queries

checkifuniquenumberexists: function(num) { 
    con.connect(function(err) { 
     if (err) throw err; 
     console.log("Connected!"); 
     var query = con.query('select id from uniquereviewtoken where token in (?)', num, function(err, result, fields) { 
      // Neat! 
      //console.log(query); 
      console.log("error", err); 
      console.log("result", result.length); 
      console.log("fields", fields); 
      return result; 


     }); 

    });