I am trying to access a nested JSON
object, but I am getting Cannot read property 'module' of undefined
.
Here is the JSON
file.
{
"server": {
"module": {
"InPluginPath": "/usr/home/nah/Website/server/httpModule.js"
}
}
}
Then when I try to access the JSON
object after reading the file with fs.readFile()
, I get the Cannot read property 'module' of undefined
, error. Below is the line that causes the error.
console.log(config.server.module.InPluginPath);
You need to JSON.parse()
the resulting string from fs.readFile()
. For example:
fs.readFile('/tmp/foo.json', { encoding: 'utf8' }, function(err, data) {
if (err) throw err;
try {
data = JSON.parse(data);
} catch (ex) {
console.log('Error parsing json');
return;
}
console.log(data.server.module.InPluginPath);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.