(JavaScript) data.split('\n') 不是函数
(JavaScript) data.split('\n') is not a function
我正在尝试制作一个随机字符串选择器,其中包含一个在每一行都拆分的文本文档。
然而,当我尝试 运行 它时,它说 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
函数。所以你需要指定一个encoding
来得到文本结果。
fs.readFile('list.txt', 'utf8', ...)
我正在尝试制作一个随机字符串选择器,其中包含一个在每一行都拆分的文本文档。
然而,当我尝试 运行 它时,它说 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
函数。所以你需要指定一个encoding
来得到文本结果。
fs.readFile('list.txt', 'utf8', ...)