0

In my nodeJS app, Im trying to build a helper library that connects to my external service.

I want to get to


    const client = require('./myService')(serviceKey);

in the app I want to be able to call multiple functions, eg:


    var healthcheckState = client.healthcheck();
    var functionOneBool = client.someFunction('variable0','variable1');

I found several SO posts about how to do this; How to pass variables into NodeJS modules? How can I pass a variable while using `require` in node.js?

But I couldn't figure out how to adapt them.

This is myService.js


    module.exports = function(serviceKey) {
        var modules = {};
        modules.healthcheck = {
            function(){
                console.log('I have a heartbeat!');
            }
        };
        return modules;
      };

When I try to run:


    const client = require('./myService')('abc123');
    client.healthcheck();

Im told that client.healthcheck is not a function

Where did I go wrong?

Mathew Jenkinson
  • 844
  • 2
  • 11
  • 18
  • 1
    Let's think out loud. 1. the require method is returning a value, in your case its returning modules, which you have set as an object in your myService.js, You're also setting a property of 'healthcheck' which you're defining as an object. So far we see you're returning: 'modules' as an object with a module.healthcheck as an object. So you're modules.healthcheck as an object is not callable and is therefore not a function. An minimum, so far you should redefine module.healthcheck as a function expression instead of an object. – Jason Ashley Jun 26 '19 at 21:32

2 Answers2

1

There is a Syntax error in your code. You are making a modules object, and in module Object you are making an object without any key, and function as value.

Basically you are making: obj = { healthcheck: { func } }; You need it ot be obj = { healthcheck: func }

module.exports = function(serviceKey) {
    const modules = {};
    modules.healthcheck = function(){
            console.log('I have a heartbeat!');
        }
    return modules;
};
Aritra Chakraborty
  • 12,123
  • 3
  • 26
  • 35
0

Setting up a unit test for this scenario would not be an ideal test. What about placing a setter function on your service?

let client = require('./myService');
client.setServiceKey('abc123');
// or
client.setServiceKey(process.env.SERVICE_KEY);

let myReturnValue = client.myDifferenctCalls();

Then you can set up a nice, tokenValid or tokenNotValid unit test to go with it.

Next think it through 1. your require method returns a value, in your case its returning modules as an object in your myService.js, You're also setting a property of 'healthcheck' which you're defining as an object. So far we see you're returning: 'modules' as an object with a prop module.healthcheck as an object. So your modules.healthcheck as an object is not [[callable]] and is therefore not a function. At minimum, so far you should redefine module.healthcheck as a function expression instead of an object. So I've given the answer in writing, but you need to code it.

Jason Ashley
  • 631
  • 6
  • 8