NodeJS:服务必须使用 `new` 运算符构建

NodeJS: service must be constructed with `new` operator

cI正在尝试使用 aws-sdk 创建一个 class,我得到:

 Service must be constructed with `new` operator

代码如下:

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

我是这样称呼它的……

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

我不知道我错过了什么......

它在抱怨,因为您试图在 switch 中没有 new 的情况下实例化服务。例如:

conn = aws.DYNAMODB(config)

应该是:

conn = new aws.DynamoDB(config)

这当然假设您在某个地方调用过:

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