Sona Shetty
Sona Shetty

Reputation: 1047

Issue with nodejs callback function

I am new to nodejs and java script.

I am trying to read a config.json file in nodejs project using the below code snippet.whenever i run the program it's giving an error 'TypeError: Cannot set property 'getProjectSettings' of undefined'

Can some one help me to find the issue with the code?

var Env = "DEV"

function getConfigValue(configKey, subConfigKey, isblnEnvattr, callback) {

    return callback(configKey, subConfigKey, isblnEnvattr);

}

function readConfigJson(configKey, subConfigKey, isblnEnvattr) {

    if (Boolean(isblnEnvattr) == true) { //eg MONGODB_DEV
        configKey = configKey + "_" + Env;
    }

    try {
        return 'x';
    } catch (err) {
        return "key Not found";
    }
}

module.export.getProjectSettings = function (configKey, subConfigKey, isblnEnvattr) {
    return getConfigValue(configKey, subConfigKey, isblnEnvattr, readConfigJson)
}

getProjectSettings("Primary","secondary",false)

Upvotes: 0

Views: 49

Answers (1)

Are
Are

Reputation: 2241

You have a typo - it should be module.exports, not module.export.

module.exports.getProjectSettings = function (configKey, subConfigKey, isblnEnvattr) {
    return getConfigValue(configKey, subConfigKey, isblnEnvattr, readConfigJson)
}

Also, you can skip module before export, as long as you are not trying to export only one function (like such exports = function () { ... }).

exports.getProjectSettings = function (...) { ... } 

Upvotes: 4

Related Questions