biergardener
biergardener

Reputation: 185

node.js callback of an export typeerror undefined

I'm having difficulties getting a value returned from this function in a separate file called InfoHandler.js which gets included in my second file.

module.exports = {
    getInfo : function (val, callback) {
            jsonRPC.getInfo(function(json){
                    Data1 = json.result[2];
                    Data2 = json.result[0];
                    Data3 = val;
                    json = JSON.stringify(json);
                    console.log(json);
                    callback(json)
            });
    },

I want to use this like this:

require("InfoHandler.js");

var Info = InfoHandler.getInfo('50');

Info should now contain the json string which gets prepared from the getInfo function via the callback. Unfortunately I get:

TypeError: undefined is not a function

for the callback.

It's most likely an Async IO Problem, can someone give me a hint ?

Upvotes: 2

Views: 97

Answers (2)

user5853538
user5853538

Reputation:

You need to pass a callback function to getinfo().

Instead of:

var Info = InfoHandler.getInfo('50');

Try:

InfoHandler.getInfo('50', function(Info) {
    //Use Info here.
});

Upvotes: 1

Mike Cluck
Mike Cluck

Reputation: 32511

It's not an async IO problem, you are trying to call undefined as a function. You aren't passing a callback to getInfo so callback is undefined. Instead, do something like this:

InfoHandler.getInfo('50', function(json) {
  console.log(json);
});

Upvotes: 1

Related Questions