2016-11-19 4 views
-1

ディレクトリの内容を調べ、ディレクトリ内のすべてのxmlファイルを返す関数を作成しようとしています。これまでのところ私は、ディレクトリ内のすべてのファイル(console.log(files)プリントアウトファイル名の文字列の配列を返すことができるが、私は私が取得していますpath.extname機能とフィルタリングしようとすると:未処理の拒否TypeError:path.extnameが関数ではありません

usr/local/Cellar/node/6.8.0/bin/node /Users/shooshte/Sportradar/notThatKindOfPeon/bluebird.js Unhandled rejection TypeError: path.extname is not a function at /Users/shooshte/Sportradar/notThatKindOfPeon/bluebird.js:23:31 at Array.filter (native) at /Users/shooshte/Sportradar/notThatKindOfPeon/bluebird.js:22:30 at tryCatcher (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/util.js:16:23) at Promise._settlePromiseFromHandler (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/promise.js:510:31) at Promise._settlePromise (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/promise.js:567:18) at Promise._settlePromise0 (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/promise.js:612:10) at Promise._settlePromises (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/promise.js:691:18) at Promise._fulfill (/Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/promise.js:636:18) at /Users/shooshte/Sportradar/notThatKindOfPeon/node_modules/bluebird/js/release/nodeback.js:42:21 at FSReqWrap.oncomplete (fs.js:123:15)

これは私ですコード:

const Promise = require('bluebird'); 
const fs = Promise.promisifyAll(require('fs')); 
const path = require('path'); 

function getFileNames(path) { 
    // Read content of path 
    return fs.readdirAsync(path) 
     // For every file in path 
     .then(function(content) { 
     // Filter out the directories 
     return content.filter(function(file) { 
      return fs.statSync(path + '/' + file).isDirectory(); 
     }); 
     }) 
     // For every directory 
     .then(function(directories) { 
     directories.map(function(directory) { 
      // Read file in the directory 
      fs.readdirAsync(path + '/' + directory + '/') 
       .then(function(files) { 
       // Filter out the XMLS 
       return files.filter(function(file) { 
        return path.extname(file) == '.XML'; 
       }); 
       console.log(files); 
       }); 
      }); 
     }); 
     } 

getFileNames('./XML'); 

答えて

1

次の2つの異なるもののためpathを使用している、と彼らはお互いに干渉しているあなたはグローバルモジュールを持っている:。

const path = require('path'); 
// ---^ 

...しかし、あなたは、引数を指定していることをシャドウ:(

function getFileNames(path) { 
// -------------------^ 

のでgetFileNames内、path識別子はその引数ではなく、自分の世界を意味し、そしてそれがpathモジュールを参照していないので、コンテキストから、私はpath引数が文字列であると推測しています)、あなたはpath.extnameを持っていません。

異なる名前を使用してください。

関連する問題