Skip to content Skip to sidebar Skip to footer

JSON Error When Parsing "... Has No Method 'replace'"

Let me preface this with the admission that I am a complete programming and javascript noob and that fact is the source of my trouble. I'm trying to populate a large array of custo

Solution 1:

fs.readFile takes 2 or 3 arguments, when passing only the filename and a callback, then your callback function will get the following two arguments (err, data) where data is a raw buffer.

So the right way to do it would be:

fs.readFile('/savedcustomobjectarray', function (err, data) {
  var customobjectarray = JSON.parse(data.toString('utf8'));
});

data.toString takes the encoding as the first argument.

Alternitavley you could specify the encoding as the second argument to the fs.readFile and have it pass a string to the callback:

fs.readFile('/savedcustomobjectarray', 'utf8', function (err, data) {
  var customobjectarray = JSON.parse(data);
});

Node API docs is your best friend!


Post a Comment for "JSON Error When Parsing "... Has No Method 'replace'""