Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node module.export and recursion

Tags:

node.js

I am working on a node app that essentially is a simple AWS SQS poller that should sit and listen to new items in different queues.

Here is my module.export:

module.exports = {
    readMessage: function(qParams, qType, tableName) {
        logger.debug(qType);

        SQS.receiveMessage(qParams, handleSqsResponse);

        function handleSqsResponse (err, data) {
            if(err) logger.error("handleSqsResponse error:" + err);
            if (data && data.Messages) {
                data.Messages.forEach(processMessage)
                readMessage(); // continue reading until draining the queue (or UPTIME reached)
            }
            else{
                logger.debug("no data in sqs.");
                // process.exit();
            }
        }

        // 'processing' is mainly writing to logs using winston. Could add here any transformations and transmission to remote systems
        function processMessage(sqsMessage){
            // Parse sqs messag
            var msgObj = JSON.parse(sqsMessage.Body);

            // Process
            logger.info(msgObj.Message);

            _.extend(qParams, { "ReceiptHandle": sqsMessage.ReceiptHandle });

            dbMap[qType](msgObj, qParams, tableName);
        }
    }
}

The issue I am running into is when I attempt to call readMessage(); again. I get the error of ReferenceError: readMessage is not defined

like image 645
dennismonsewicz Avatar asked Oct 23 '14 20:10

dennismonsewicz


2 Answers

module.exports is a plain object that is exposed to outer modules that has a method readMessage. readMessage() should be module.exports.readMessage().

Also i would suggest creating a variable and then exporting that:

var obj = {
    readMessage: function(qParams, qType, tableName) {
        logger.debug(qType);

        SQS.receiveMessage(qParams, handleSqsResponse);

        function handleSqsResponse (err, data) {
            if(err) logger.error("handleSqsResponse error:" + err);
            if (data && data.Messages) {
                data.Messages.forEach(processMessage)
                obj.readMessage(); // continue reading until draining the queue (or UPTIME reached)
            }
            else{
                logger.debug("no data in sqs.");
                // process.exit();
            }
        }

        // 'processing' is mainly writing to logs using winston. Could add here any transformations and transmission to remote systems
        function processMessage(sqsMessage){
            // Parse sqs messag
            var msgObj = JSON.parse(sqsMessage.Body);

            // Process
            logger.info(msgObj.Message);

            _.extend(qParams, { "ReceiptHandle": sqsMessage.ReceiptHandle });

            dbMap[qType](msgObj, qParams, tableName);
        }
    }
}

module.exports = obj;

Please note that I only responded to the question you specifically asked. I didn't take into account any architectural issue associate with the code.

like image 173
Maroshii Avatar answered Nov 15 '22 21:11

Maroshii


function functionName(has = false){
  var total = 0;
  if(has){
    functionName(true)
  } else {
    // Todo
  }
}

module.exports.functionName = functionName;
like image 42
Rahman Qadirzade Avatar answered Nov 15 '22 21:11

Rahman Qadirzade