Reputation: 13
I am currently trying to read a file using fs
in Node v12.16.2. I currently have a function setup like so:
const fs = require('fs');
var JSONData;
const read = function(path){
fs.readFile(path, 'utf8', (data, err) => {
if(err){
console.log(err);
console.log('there was an error');
}
JSONData = data;
})
}
read('./path/to/file.json');
In the console, I simply get
{}
there was an error
I also tried console.log
ing err.message
, err.valueOf
, and using throw err
, none of which gave me any more data on the error. I'd appreciate if someone could help either discover my problem or knows it already.
Upvotes: 1
Views: 848
Reputation: 3816
Callbacks in Node.js receive the error as the first argument:
const fs = require('fs');
var JSONData;
function read (path) {
fs.readFile(path, 'utf8', (err, data) => { // <-- Look here
if (err) {
console.log(err);
console.log('there was an error');
}
JSONData = data;
})
}
read('./path/to/file.json');
Upvotes: 2