我有一段代码检查.json的存在。我需要代码仅在特定文件夹中查找.json。我该怎么做?

if (extName === '.json') {
    const fullFileNamed = '../../theme-default/resource/i18n'
    // console.log (fullFileNamed, "fullFileNamed")

    if(fullFileNamed) {
      console.log ( fullFileNamed, "fullFileNamed")
      const fileContent = fs.readFileSync(fullFileName, 'utf8')
      console.log (fileContent, "fileContent")
    }
}


这是不正确的。怎么做对?

最佳答案

if条件很好,变量名称在const fileContent = fs.readFileSync(fullFileName, 'utf8')中拼写错误,应将其更正为const fileContent = fs.readFileSync(fullFileNamed, 'utf8')

if (extName === '.json') {
    const fullFileNamed = '../../theme-default/resource/i18n'
    // console.log (fullFileNamed, "fullFileNamed")

    if(fullFileNamed) {
      console.log ( fullFileNamed, "fullFileNamed")
      const fileContent = fs.readFileSync(fullFileNamed, 'utf8')
      console.log (fileContent, "fileContent")
    }
}

关于javascript - 如何查看目录?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59389893/

10-12 16:24