Abhinav Abhinav - 3 months ago 25
Javascript Question

Nodejs - Error using properties-reader package

I am trying to write a generic properties reader utility using

properties-reader
package of
nodejs
. Following is implementation:

var propertiesReader = require('properties-reader')
var path = require('path')

var property = getPropertyFile('./fileName.properties');
console.log(getProperty(property,'user1.username'))

function getPropertyFile(err, filePath){
if(err){
console.log("Error occured while reading properties file: "err);
}
else{
return propertiesReader(filePath);
}
}

function getProperty(err, propertyFile, propertyName){
if(err){
console.log("Error occured while getting properties: ",err);
}
else{
return propertyFile.get(propertyName)
}
}


on executing above code I am getting error in reading file and getting property value as below:

E:\NodeJS\>node propertiesUtility.js
Error occured while reading properties file: ./locatorValues.properties
E:\NodeJS\propertiesUtility.js:24
return propertyFile.get(propertyName)
^

TypeError: propertyFile.get is not a function
at getProperty (E:\NodeJS\propertiesUtility.js:24:23)
at Object.<anonymous> (E:\NodeJS\propertiesUtility.js:5:13)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:467:10)
at startup (node.js:136:18)
at node.js:963:3


Can someone please help me in correcting my code.

Answer

I made some change like below and it worked:

function getValueFromPropertyFile (filePath, propertyName){
        try{
            property = propertiesreader(filePath)
            return property.get(propertyName)
        }
        catch(e){
            if (e.code !== 'ENOENT') {
                throw e
            } else {
            }
        }       
    }