我正在尝试使用在每一行分割的文本文档制作一个随机字符串选择器。
但是,当我尝试运行它时,它说 data.split
不是一个函数。
fs.readFile('list.txt', function(err, data){
if(err) throw err;
var lines = data.split('\n');
var rand = [Math.floor(Math.random()*lines.length)];
var rlist = lines[rand]
})
最佳答案
如 documentation 中所述:
The callback is passed two arguments (err, data), where
data
is the contents of the file. If no encoding is specified, then the raw buffer is returned.
原始缓冲区只是一个数组,而 JavaScript arrays没有 split
功能。因此,您需要指定编码
来获取文本结果。
fs.readFile('list.txt', 'utf8', ...)
关于javascript - (JavaScript) data.split ('\n' ) 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43701291/