我试图通过传递令牌号来搜索mysql数据库,并且我想从数据库返回附加了该令牌的id。 我已经尝试但我看不到任何结果集,我无能为力,因为我做错了什么。 这是我到目前为止所尝试的。
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 } ]
答案 0 :(得分:1)
您不需要post
个对象。
参考: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;
});
});