最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: val.slice is not a function - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

This 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}`;
发布评论

评论列表(0)

  1. 暂无评论