Hello I'm trying to read a JSON file using nodejs and when I try to access one of the properties they come back as undefined. However, when I console.log the entire file is shows up.
var keyFile;
function setKeys(callback){
const fs = require('fs');
const filePath = '../KEY.json';
fs.readFile(filePath, 'utf-8', (error, data) => {
if (error){
return console.log(error);
}
keyFile = data;
callback();
});
}
setKeys(()=>{
console.log(keyFile) // shows JSON
console.log(keyFile.google) //undefined
});
KEY.json:
{
"darksky": "ab",
"google": "cd"
}