buffer.js:246 "Object 1 has no method 'toLowerCase'
Problem
I m trying to make a .smil (.xml) parser in javascript. But when I want to test it, node.js just say me that:
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 () is indeed one of my function:
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;}
}
});
}
I just don t have any idea of what go wrong, it seem to be read, but I ve made sure to get the right arguments, tried to update node.js, fs and npm. And the only similar error I found on google was a update problem.
EDIT: Added the complete error message, here function 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, "");
...
Solution
fs.read
takes a buffer not a string.
Change your cache to be a buffer.
function gets (file){
var chaine="", cache=new Buffer(), 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;}
}
});
}
See the fs.read code here
If you want to use a string as a "buffer" then you must use the legacy interface
legacy string interface fs.read(fd, length, position, encoding, callback)
Discussion
View additional discussion.