Simply  Seth
Simply Seth

Reputation: 3506

NodeJS: service must be constructed with `new` operator

cI'm trying to create a class with with aws-sdk and I get:

 Service must be constructed with `new` operator

Here is the code:

Class AWS {
    // constructor omitted 

    connect({client='ecs'}={})
        {
          let config =
            {
              accessKeyId: this.aws_access_key,
              secretAccessKey: this.aws_secret_key,
              region: this.region
            };

          try {
            switch (client) {
              case 'dynamodb': conn = aws.DYNAMODB(config); break;
              case 'ec2':      conn = aws.EC2(config);      break;
              case 'ecs':      conn = aws.ECS(config);      break;
              case 'elb':      conn = aws.ELBv2(config);    break;
            }
            return conn;
          }
          catch(err)
          {
            console.log(err.message);
          }
        }
}

Here is how I'm calling it ...

var a = new AWS(
    {
      cluster: 'my-cluster',
      environment: 'dev',
      project: 'proj1',
      region: 'us-east-2',
      service: 'api-feed-validation'
    });
a.connect({client:'ecs'})

I'm at a loss as to what I'm missing ...

Upvotes: 0

Views: 1993

Answers (1)

Mark
Mark

Reputation: 92461

It is complaining because you are trying to instantiate the services without new in you switch. For example:

conn = aws.DYNAMODB(config)

should be:

conn = new aws.DynamoDB(config)

this of course assumes that somewhere you have called:

var aws = require('aws-sdk');

Upvotes: 4

Related Questions