I'm using a simple Node.js to pull information from a valid jsonfile (checked with JSLint), but the code i'm using doesn't return the expected value:
squadJSON = JSON.parse(fs.readFileSync('./squads/squad' + whichSquad + '.json'));
and it returns:
{ type: 'Buffer', data: [ 123, 10, 32, 32, 34, 97, 99, ... 548 more items ] }
Any reason as to why this happens?
readFileSync() returns a Buffer if you don't specify an encoding. Yup!
The readFileSync() function returns a Buffer .
The readFileSync() method will read the content of a file synchronously, so your JavaScript code execution will be stopped until the method is finished. The readFileSync() method accepts two parameters: path - mandatory - which is the relative path to the file you want to read ( string type)
fs.readFileSync()
returns a Buffer if you don't specify an encoding.
https://nodejs.org/api/fs.html#fs_fs_readfilesync_path_options
So, tell fs.readFileSync()
what encoding to use:
squadJSON = JSON.parse(fs.readFileSync('./squads/squad' + whichSquad + '.json', 'utf8'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With