未捕获的TypeError:无法构造“文件”:需要2个参数,但仅存在1个

时间:2018-11-16 20:04:16

标签: javascript html google-chrome

我正在编写JavaScript,以使用Google Chrome浏览器将文件写入到一个aframe虚拟现实应用程序中的本地目录中。

在调试时出现此错误:

Uncaught TypeError: Failed to construct 'File': 2 arguments required, but `only 1 present.`

我已经从其他人那里获得了以下代码,但我不明白为什么Google Chrome浏览器会说需要2个参数?

<script>
/// write to file
var txtFile = "skycolour.txt";
var file = new File(txtFile);
var str = "My string of text";

file.open("w"); // open file with write access
file.writeln("First line of text");
file.writeln("Second line of text " + str);
file.write(str);
file.close();
</script>

感谢您的帮助。

Image of the error for your reference

1 个答案:

答案 0 :(得分:1)

// Requiring fs module in which 
// writeFile function is defined. 

 const fs = require('fs') 

// Data which will write in a file. 

let data = "Learning how to write in a file."

// Write data in 'Output.txt' . 

fs.writeFile('Output.txt', data, (err) => { 

   // In case of a error throw err. 

    if (err) throw err; 
})