不了解Node中的module.exports

时间:2014-10-09 00:46:11

标签: node.js

我正在开发一个应用程序,该应用程序将从SQS队列中取出项目并将它们放在单个DynamoDB实例中。

这是我的module.exports文件:

"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;
        }
    }
}

我这样称呼这个文件:

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);
            }
        });

    });

由于某种原因,我无法访问导出对象中任何其他位置的init()函数中设置的任何变量。例如,我的this.tblName方法无法在init方法中设置createActivity

0 个答案:

没有答案