gpt4 book ai didi

javascript - TypeError : val. 切片不是函数

转载 作者:行者123 更新时间:2023-11-29 10:39:31 24 4
gpt4 key购买 nike

在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”或切片函数。

最佳答案

这表明您的 pool.escape 调用的参数不是字符串。 slice 用于实现该 escape 方法。

共有三个候选调用可能导致此错误:

pool.escape(row[i].csteamid) 
pool.escape(makeTID)
pool.escape(cod)

调试您的代码以查看其中之一是否(有时)不是字符串(例如 nullundefined 或对象,...)

您可以强制参数为这样的字符串,尽管这几乎肯定不会给出所需的结果:

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}`;

关于javascript - TypeError : val. 切片不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45775520/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com