Node.js path的参数.Join必须是字符串,但参数是路径
Node.js Arguments to path.join must be strings but arguments are a path
我使用Node.js版本v0.10.36中的路径模块。它在我的笔记本电脑上的Node.js版本v0.10.30。我基本上是传递一个对象中包含的字符串,其中一个返回值来自Sequelize处理的函数,从MySQL数据库中获取。如果我把路径换掉。将函数连接到常规路径+'/'+文件,它可以正常工作。然而,由于某种原因路径。Join不断在程序中抛出错误。我添加了一些控制台。用原木来说明我的观点。错误来自以下几行:
console.log( this.recordingRow.get( 'id' ) );
console.log( config.path.audio );
var filepath = path.join( config.path.audio, this.recordingRow.get( 'id' ) );
但是这里是输出:
241
/home/sonar/sonar/files/audio
Unhandled rejection TypeError: Arguments to path.join must be strings
at path.js:360:15
at Array.filter (native)
at Object.exports.join (path.js:358:36)
at Stream.startSonarData (/home/sonar/sonar/inc/streams/stream.js:148:22)
at Object.<anonymous> (/home/sonar/sonar/inc/streams/stream.js:74:8)
at Object.tryCatcher (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/util.js:24:31)
at Promise._settlePromiseFromHandler (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:452:31)
at Promise._settlePromiseAt (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:530:18)
at Promise._settlePromises (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/promise.js:646:14)
at Async._drainQueue (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/async.js:177:16)
at Async._drainQueues (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/async.js:187:10)
at Async.drainQueues (/home/sonar/sonar/node_modules/sequelize/node_modules/bluebird/js/main/async.js:15:14)
at process._tickCallback (node.js:442:13)
这个错误很明显。其中一个参数不是字符串。第一个似乎是一个数字。只需先将其转换为字符串,例如通过String
:
path.join(a, String(b));
如@Felix所述,您的第二个参数不是字符串而是数字。有两种方法可以将数字类型转换为String:-
- 路径。加入(config.path。音频,~ ~ (this.recordingRow。get('id'));
- 路径。加入(config.path。音频、字符串(this.recordingRow。get('id'));
相关文章:
- 从查询字符串参数推断出正确的数据类型
- 如何使用JQuery在HTML中创建包含字符串参数的引号的onclickjavascript链接
- 从带参数的字符串变量调用函数中的函数
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何格式化match()参数以定位电子邮件中的字符串
- 将JSON字符串转换为函数参数
- 如何用javascript替换Jquery字符串上的新参数
- 模版启动错误-类型错误(“参数”url'必须是字符串,而不是“+类型的url”)
- 在 URL 字符串中传递参数字符串
- 如何在附加的 html 中的字符串上连接 javascript 函数中的参数字符串
- jQuery:从参数字符串设置表单状态
- 使用 cookie 传递 URL 参数字符串
- 寻找一个正则表达式来解析 JS 的参数字符串
- 有必要限制node.js中GET请求参数字符串的大小吗
- 使用jquery获取javascript中的参数字符串
- 使用JavaScript更改url参数字符串
- 如何在提交GET表单后将变量插入到生成的url参数字符串中
- JavaScript:向JavaScript函数发送以逗号分隔的参数字符串
- 无法通过XMLHttpRequest发送参数字符串
- 从Javascript中获取url参数字符串