2017-06-26 1 views
1

私はNodejsを初めて使いこなしていますが、この問題で困っています。これはNodeJsの変数スコープに関連していると確信していますが、いつか私の頭を掴むことができませんでした。Nodejs関数と変数スコープの呼び出しに問題があります。 ConfigError:設定の範囲がありません

号 - >なぜ次のプログラムの出力は以下です:

ConfigError: Missing region in config

##server.js## 

`const aws = require('aws-sdk'); 
const dynamoConfig = require('./database'); 
const dynamodbClient = new aws.DynamoDB(); 
const dynamoDocClient = new aws.DynamoDB.DocumentClient(); 

aws.config.update({ 
     region: dynamoConfig.region, 
     endpoint: dynamoConfig.url 
    }); 

function userRegistrationDB(tableName) { 
    this.tableName = tableName; 
}; 

userRegistrationDB.prototype.init = function() { 
    // body... 
    aws.config.update({ 
     region: dynamoConfig.region, 
     endpoint: dynamoConfig.url 
    }); 
}; 

userRegistrationDB.prototype.putItem = function(userid, username) { 
    // body... 
    var param = { 
     TableName: this.tableName, 
     Item: { 
      "id": userid, 
      "name": username 
     } 
    }; 

    console.log('updating item in table '+this.tableName); 
    var res = dynamoDocClient.put(param, function(err, data){ 
      if(err) console.log('error ->'+err); 
      else console.log(data+"success"); 
     }); 
}; 

module.exports = userRegistrationDB; 

var obj = new userRegistrationDB('UserRegistration'); 
obj.init(); 
obj.putItem(123,'test2'); 

##database.js## 
    module.exports ={ 
    'region': 'us-west-2', 
    'url': 'http://localhost:8000' 
}; 

は、どちらかの機能が正しく値を取得することができ、またdatabase.jsを形成:

aws.config.update({ 
    region: dynamoConfig.region, 
    endpoint: dynamoConfig.url 
}); 

userRegistrationDB.prototype.init = function() { 
    // body... 
    aws.config.update({ 
     region: dynamoConfig.region, 
     endpoint: dynamoConfig.url 
    }); 
}; 

答えて

0

設定の更新後にdynamodbClientdynamoDocClient行を移動します。

例: -

aws.config.update({ 
     region: dynamoConfig.region, 
     endpoint: dynamoConfig.url 
    }); 
const dynamodbClient = new aws.DynamoDB(); 
const dynamoDocClient = new aws.DynamoDB.DocumentClient(); 
+0

おかげで、このために働く: aws.config.update({ 領域:dynamoConfig.region、 エンドポイント:dynamoConfig.url })。 今、この関数を削除して、db.initがそれを行うことを期待します。 その場合の解決策は何ですか? –

関連する問題