node module.export and recursion

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.


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

module.exports.functionName = functionName;

Tags:

Node.Js