0

I am working on an app that will be taking items off of SQS queues and placing them in individual DynamoDB instances.

Here is my module.exports file:

"use strict";

var AWS = require('./aws'),
    DynamoDB = new AWS.DynamoDB(),
    _ = require('underscore');

module.exports = {
    init: function(qType, qObj, tableName) {

        if(_.isEmpty(qObj)) {
            this.errors.push("Object to insert is empty");
            return;
        }

        this.dynamo = DynamoDB;
        this.dbMap = {
            'users': this.createUser,
            'activities': this.createActivity,
            'authentications': this.createAuthentication
        };

        this.tblName = tableName;
        this.errors = [];

        this.dbMap[qType](qObj);
        this.processErrors();
    },

    createActivity: function (obj) {
        this.dynamo.putItem({
            "TableName": this.tblName,
            "Item": {
                "UserId": { "N": obj.user_id.toString() },
                "CampaignId": { "N": obj.campaign_id.toString() },
                "Email": { "S": obj.email },
                "CustomActivityNodeId": { "N": obj.custom_activity_node_id.toString() }
            }
        }, function (err, data) {
            if(err) {
                this.errors.push(err);
            }
        });
    },

    createAuthentication: function (obj) {
        this.dynamo.putItem({
            "TableName": this.tblName,
            "Item": {
                "UserId": { "N": obj.user_id.toString() },
                "CampaignId": { "N": obj.campaign_id.toString() },
                "Provider": { "S": obj.provider },
                "UID": { "S": obj.uid },
                "OauthToken": { "S": obj.oauth_token },
                "OauthTokenSecret": { "S": obj.oauth_token_secret },
                "Nickname": { "S": obj.nickname }
            }
        }, function (err, data) {
            if(err) {
                this.errors.push(err);
            }
        });
    },

    createUser: function(obj) {
        this.dynamo.putItem({
            "TableName": this.tblName,
            "Item": {
                "UserId": { "N": obj.user_id.toString() },
                "Identifier": { "S": obj.identifier },
                "ReferralToken": { "S": obj.referral_token },
                "CampaignId": { "N": obj.campaign_id.toString() },
                "FirstName": { "S": obj.first_name },
                "LastName": { "S": obj.last_name },
                "Gender": { "S": obj.gender },
                "BirthDate": { "S": obj.birthdate },
                "Username": { "S": obj.username },
                "MobileNumber": { "S": obj.mobile_number },
                "PostalCodeText": { "S": obj.postal_code_text },
                "Classification": { "S": obj.classification },
                "DeliveryEmail": { "B": obj.delivery_email },
                "DeliverySMS": { "B": obj.delivery_sms }
            }
        }, function (err, data) {
            if(err) {
                this.errors.push(err);
            }
        });
    },

    processErrors: function() {
        if(!_.isEmpty(this.errors)) {
            return this.errors;
        }
    }
}

I am calling this file like so:

var config = require('./config/config'),
        fs = require('fs'),
        sqsQueueURLs = config.sqs.urls,
        _ = require('underscore'),
        sqs = require('./lib/sqs'),
        dynamo = require('./lib/dynamodb');

    config.sqs.queues.forEach(function(element, index) {
        var key = element.split("-").pop(),
            tableName = config.dynamo[key],
            params = {
                QueueUrl: config.sqs.urls[key],
                VisibilityTimeout: 60
            };

        sqs.receiveMessage(params, function(err, data) {
            if(err) console.log(err);

            if(data.Messages) {
                var message = data.Messages[0],
                    body    = JSON.parse(message.Body);

                dynamo.init(key, body, tableName);
            }
        });

    });

For some reason I can't access any variables set in the init() function anywhere else in the exported object. For example, setting this.tblName in the init method isn't accessible in my createActivity method.

dennismonsewicz
  • 25,132
  • 33
  • 116
  • 189
  • How are you calling the other non-`init()` functions? Also, why not just use free variables that you define outside of your `module.exports` functions? – mscdex Oct 09 '14 at 02:20
  • Read this first to understand how `this` works: http://stackoverflow.com/questions/13441307/how-does-the-this-keyword-in-javascript-act-within-an-object-literal/13441628#13441628 – slebetman Oct 09 '14 at 04:16

0 Answers0