Node.js path.join的参数必须是字符串,但参数是路径

时间:2015-05-30 05:05:19

标签: javascript node.js types sequelize.js

我在Node.js版本v0.10.36中使用路径模块。它可以在我的笔记本电脑上运行Node.js版本v0.10.30。我基本上传递一个对象中包含的字符串,其中一个返回值来自Sequelize处理的从MySQL数据库获取的函数。如果我将path.join函数切换到常规路径+' /' +文件,它可以正常工作。但是,由于某些原因,path.join不断在程序中抛出错误。我添加了一些console.log来说明我的观点。错误来自以下几行:

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)   

2 个答案:

答案 0 :(得分:0)

嗯,错误非常明显。其中一个参数不是字符串。第一个似乎是一个数字。首先将其转换为字符串,例如通过<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

String

答案 1 :(得分:0)

如@Felix所述,你的第二个参数不是字符串而是数字。有两种方法可以将数字强制转换为字符串: -

  • path.join(config.path.audio,~~(this.recordingRow.get(&#39; id&#39;)));
  • path.join(config.path.audio,String(this.recordingRow.get(&#39; id&#39;)));