我正在尝试在 javascript 中创建一个 .smil (.xml) 解析器。但是当我想测试它时,node.js 只是告诉我:
buffer.js:246
switch(encoding && encoding.toLowerCase()){
^
TypeError: Object 1 has no method 'toLowerCase'
at Function.Buffer.isEncoding (buffer.js:246:32)
at assertEncoding (fs.js:98:27)
at Object.fsread (fs.js:422:5)
at gets (/home/pi/SMIL_Parser.js:8:8)
at read_until (/home/pi/SMIL_Parser.js:28:14)
at home/pi/SMIL_Parser.js:64:14
at Object.oncomplete (fs.js:93.15)
gets() 确实是我的功能之一:
var io=require('fs');
...
function gets (file){
var chaine="", cache="", pkmn=0;
io.read(file, cache, 0, 1, null, function(err, byte, buf){
if (err || byte===0){return -1;}
while ((cache!=="\n"))
{
chaine=chaine+cache;
cache="";
pkmn=io.readSync(file, cache, 0, 1, null);
if (pkmn===0){return -1;}
}
});
}
我只是不知道出了什么问题,它似乎已被阅读,但我确保得到正确的参数,并尝试更新 node.js、fs 和 npm。我在谷歌上发现的唯一类似错误是更新问题。
编辑:添加了完整的错误消息,这里是函数 read_until:
function read_until(smil, limit){
var line="";
do
{
line=gets(smil);
if (line===-1){return -1}
}while (!(line.search(limit)));
return 0;
}
.
function parse (pathname){
var smil=0, line="", pkmn=0;
io.open(pathname, 'r', function (err, fd){
if (err){return -1;}
smil=fd;
pkmn=read_until(smil, "<smil>");
...