节点模块:模块内的函数调用不起作用

时间:2018-09-24 06:13:35

标签: node-modules

在以下代码片段中调用

ifDirectoryExistsNot(folderName + dataType)

结尾
ReferenceError: ifDirectoryExistsNot is not defined

你知道为什么吗?

var Folder = {
  deleteRecursively: (exportFromCosmosPath) => {
      const child_process = require ('child_process')
      const dirToRemove = exportFromCosmosPath
      const k = child_process.spawn('bash')
      k.stdin.end(`rm -rf "${dirToRemove}"`)
      k.once('exit', code => {
        // check the exit code
        // now you are done
      })
  },
  ifDirectoryExists: () => { 
    fs.existsSync
  },
  ifDirectoryExistsNot: (folderName, dataType) => {
    if (negateFunction(fs.existsSync)){
      console.log('creating dir..... ' + folderName + " " + dataType )
      fs.ensureDirSync(folderName, dataType)
    }
  },
  negateFunction: () => {
    return function(x) {
      return !func(x)
    }
  },
  ensureThatDirectoryExists: (folderName, dataType) => {
    ifDirectoryExistsNot(folderName + dataType)
  }
}



// my Code:
Folder.deleteRecursively('data/*')
Folder.ensureThatDirectoryExists('data/exportFromCosmos/', 'DEFAULT')
Folder.ensureThatDirectoryExists('data/uploadToBlobStorage/', dataType)

1 个答案:

答案 0 :(得分:0)

甚至必须从模块内部调用ModuleName.Function()。

在我的情况下,function()名称和this.functionname()都不起作用。

但是Modulename.Functionname()是解决方案。

这就是解决方案

ensureThatDirectoryExists: (folderName, dataType) => {
  Folder.ifDirectoryExistsNot(folderName, dataType)
}