0

I'm quite new in NodeJS, but I'm struggling also with concept of passing variables / objects between functions. I appreciate any help on what I am doing wrong.

Please, consider this code:

incoming request:

{ 
    sender: '32165498732165845',
    text: 'shopping',
    originalRequest: 
    { 
        sender: { id: '32165498732165845' },
        recipient: { id: '87971441647898' },
        timestamp: 1488196261509,
        message: { mid: 'mid.1488196261509:c7ccb7f608', seq: 36372, text: 'shopping' } 
    },
    type: 'facebook' 
}

Extracting relevant variables:

var userId = request.sender;
var listName = request.text;

bot.js:

var listOps = require('./listops/test.js');
listOps.setActive(function (userId, listName, callback) {
    console.log ('Here I expect a callback!');
    return callback; // This will send the message back to user.
});

listops/test.js:

exports.setActive = function(userId, listName, callback) {
    var message = "User number " + userId + " asks to create a list with name " + listName + ".";
    console.log(userId);
    console.log(listName);
    callback (message);
}

Now my issue is that in listOps.js the outcome of both console logs is not the value I expect, it says [Function] and undefined. Therefore I suspect, that this is a root cause for error message [TypeError: callback is not a function].

I'm using Claudia.js in Lambda.

Prifulnath
  • 463
  • 7
  • 24
  • 1
    You may want to read about callbacks first http://stackoverflow.com/a/19739852/6048928 http://stackoverflow.com/a/19756960/6048928 – RaR Feb 27 '17 at 12:12
  • 2
    You defined `setActive` as `function(userId, listName, callback)` yet in bot.js you are only passing an anonymous function as the first argument – Patrick Evans Feb 27 '17 at 12:12
  • So like this? `listOps.setActive(userId, listName, callback); if (callback) { console.log ('Here I expect a callback!'); return callback; };` – Dave de Sade Feb 27 '17 at 12:19

2 Answers2

0

Try changing your bot.js to the following:

var listOps = require('./listops/test.js');

listOps.setActive( userId, listName, function (message) {
      console.log ('message holds the result set in listops/test.js!');
});

If you want to process the message afterwards, you simply could pass it to another function:

bot.js:

var listOps = require('./listops/test.js');

var processor = function(userId, listName, message){
     ... process as desired
}

listOps.setActive( userId, listName, function (message) {
      console.log ('message holds the result set in listops/test.js!');
      process(userId, listName, message);
});
mvermand
  • 5,829
  • 7
  • 48
  • 74
  • That looks very nice, but... where is the callback then? – Dave de Sade Feb 27 '17 at 12:29
  • The callback is the third argument of listOps.setActive. – mvermand Feb 27 '17 at 12:32
  • 1
    @DavedeSade the third argument is an [anonymous function](https://en.wikibooks.org/wiki/JavaScript/Anonymous_functions) expression that in-lines the callback function right there in the method call. You can learn more about function expressions and function statements [in this MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions). – Useless Code Feb 27 '17 at 12:43
0

this is happening because in your listops/test.js file you are defining a function exports.setActive = function(userId, listName, callback) which accepts three arguments userId listName and callback while you call this function in the bot.js file you are passing only a function listOps.setActive(function (userId, listName, callback) { which is illegal as expected by the definition of the setActive function. You need to call this function as below

listOps.setActive(userId, listName, function() {
                //your stuffs here
});
nurulnabi
  • 459
  • 3
  • 11