在nodejs中进行查询时出现此错误:
[2017-08-19 19:06:55.946] [ERROR] error - TypeError: val.slice is not a function
at escapeString (/var/www/Bot/node_modules/sqlstring/lib/SqlString.js:183:23)
at Object.escape (/var/www/Bot/node_modules/sqlstring/lib/SqlString.js:53:21)
at Connection.escape (/var/www/Bot/node_modules/mysql/lib/Connection.js:270:20)
查询:
pool.query('INSERT INTO trades SET user = ' + pool.escape(row[i].csteamid) + ', tid = ' + pool.escape(makeTID) + ', status = ' + pool.escape('PendingAccept') + ', items = ' + pool.escape(Items.join('/')) + ', action = ' + pool.escape('expired') + ', code = ' + pool.escape(cod));
如何解决此问题?我没有在查询中使用“val”或切片函数。
答案 0 :(得分:0)
这表示您的pool.escape
调用的参数不是字符串。 slice
用于实现escape
方法。
有三个候选呼叫可能导致此错误:
pool.escape(row[i].csteamid)
pool.escape(makeTID)
pool.escape(cod)
调试代码以查看其中一个是(有时)不是字符串(如null
,undefined
或对象,....)
您可以强制参数为这样的字符串,尽管这几乎肯定不会产生预期的结果:
pool.escape(row[i].csteamid + '')
pool.escape(makeTID + '')
pool.escape(cod + '')
最好在创建此SQL字符串之前测试数据类型,使用:
if (typeof row[i].csteamid !== 'string') throw `row[${i}].csteamid is not a string, but ${typeof row[i].csteamid}`;
if (typeof makeTID !== 'string') throw `makeTID is not a string, but ${typeof makeTID}`;
if (typeof cod !== 'string') throw `cod is not a string, but ${typeof cod}`;