简体   繁体   中英

Missing region in config while trying to run Chai on local DynamoDB with Dynamoose

I am using Restify and I am trying to learn how to write tests my CRUD methods. I can test them on Postman without any problems when I connect to DynamoDB. For tests, I will connect to my local DynamoDB, which is running on localhost:8000.

On server.listen , I have this:

dynamoose.AWS.config.update({
  accessKeyId: config.db.aws_access_key_id,
  secretAccessKey: config.db.aws_secret_access_key,
  region: config.db.aws_region
});

if (config.env == "test") {
  dynamoose.local();
}

(I tried with calling dynamoose.local() above update as well)

When I run test with mocha --timeout 10000 command, I get the following error:

1) Uncaught error outside test suite:
     Uncaught ConfigError: Missing region in config
      at Request.VALIDATE_REGION (node_modules/aws-sdk/lib/event_listeners.js:91:45)
      at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:105:20)
      at callNextListener (node_modules/aws-sdk/lib/sequential_executor.js:95:12)
      at /Users/username/Development/projectname-api/node_modules/aws-sdk/lib/event_listeners.js:85:9
      at finish (node_modules/aws-sdk/lib/config.js:320:7)
      at /Users/username/Development/projectname-api/node_modules/aws-sdk/lib/config.js:338:9
      at SharedIniFileCredentials.get (node_modules/aws-sdk/lib/credentials.js:126:7)
      at getAsyncCredentials (node_modules/aws-sdk/lib/config.js:332:24)
      at Config.getCredentials (node_modules/aws-sdk/lib/config.js:352:9)
      at Request.VALIDATE_CREDENTIALS (node_modules/aws-sdk/lib/event_listeners.js:80:26)
      at Request.callListeners (node_modules/aws-sdk/lib/sequential_executor.js:101:18)
      at Request.emit (node_modules/aws-sdk/lib/sequential_executor.js:77:10)
      at Request.emit (node_modules/aws-sdk/lib/request.js:683:14)
      at Request.transition (node_modules/aws-sdk/lib/request.js:22:10)
      at AcceptorStateMachine.runTo (node_modules/aws-sdk/lib/state_machine.js:14:12)
      at Request.runTo (node_modules/aws-sdk/lib/request.js:403:15)
      at Request.send (node_modules/aws-sdk/lib/request.js:367:10)
      at features.constructor.makeRequest (node_modules/aws-sdk/lib/service.js:193:27)
      at features.constructor.svc.(anonymous function) [as describeTable] (node_modules/aws-sdk/lib/service.js:499:23)
      at Table.describe (node_modules/dynamoose/lib/Table.js:339:7)
      at Table.init (node_modules/dynamoose/lib/Table.js:141:10)
      at Function.compile (node_modules/dynamoose/lib/Model.js:201:9)
      at Dynamoose.model (node_modules/dynamoose/lib/index.js:39:21)
      at Object.<anonymous> (models/modelName.js:63:35)
      at require (internal/module.js:11:18)
      at Object.<anonymous> (test/testFileName.js:4:25)
      at require (internal/module.js:11:18)
      at Array.forEach (<anonymous>)
      at startup (bootstrap_node.js:188:16)
      at bootstrap_node.js:609:3

Now I know this is a problem because the dynamoose cannot configure region. But when I run it on development it works without any problems with the same piece of code. In my configuration file I have the same DB config too. I verified this by adding console.log(config.db) inside if (config.env == "test") . So I think it should be configured.

Here's my test if it's needed:

describe('ModelName', () => {
  beforeEach((done) => {
    ModelName.delete({}, (err) => {
      done();
    });
  });

  describe('/GET modelName', () => {
    it('it should GET all the model-name instances', (done) => {
      chai.request(server)
        .get('/model-name')
        .end((err, res) => {
            res.should.have.status(200);
            res.body.should.be.a('array');
            res.body.length.should.be.eql(0);
          done();
        });
    });
  });
});

Any ideas what might be the problem?

When you run locally your region will be getting picked up from a file located at

%UserProfile%\.aws\config

Which will look something like this

[default]
region=us-west-2
output=json

My guess would be that when you are running on a server the block

dynamoose.AWS.config.update({
  accessKeyId: config.db.aws_access_key_id,
  secretAccessKey: config.db.aws_secret_access_key,
  region: config.db.aws_region
});

Is not doing what you expect, but the issue is hidden when you run locally as the configs are getting picked up in your profile.

Try setting the config as a literal region: 'us-west-2' , or you could set the credentials and configs are environment variables on the server (they will get picked automatically).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM