admin管理员组文章数量:1387350
I get this error when making a query in 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)
The query:
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));
How can I fix this issue? I'm not using "val" or the slice function in the query.
I get this error when making a query in 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)
The query:
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));
How can I fix this issue? I'm not using "val" or the slice function in the query.
Share Improve this question asked Aug 19, 2017 at 19:22 Teh ToroTeh Toro 411 silver badge6 bronze badges 1- Had the same issue, it turned out that I was passing a function rather than a string. The clarity of the error message could be improved a bit. – kiwib123 Commented Apr 25, 2019 at 5:36
1 Answer
Reset to default 5This is an indication that you have a pool.escape
call with an argument that is not a string. slice
is used in the implementation of that escape
method.
There are three candidate calls that could be responsible for this error:
pool.escape(row[i].csteamid)
pool.escape(makeTID)
pool.escape(cod)
Debug your code to see whether one of these is (sometimes) not a string (like null
, undefined
or an object, ....)
You can force the argument to be a string like this, although that will almost certainly not give the desired result:
pool.escape(row[i].csteamid + '')
pool.escape(makeTID + '')
pool.escape(cod + '')
Better is to test the data type before you create this SQL string, with this:
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}`;
本文标签: javascriptTypeError valslice is not a functionStack Overflow
版权声明:本文标题:javascript - TypeError: val.slice is not a function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744516354a2610184.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论