【问题标题】:TypeError: Cannot read property 'send' of undefined AWS-SDK ec2TypeError:无法读取未定义 AWS-SDK ec2 的属性“发送”
【发布时间】:2018-02-20 02:31:59
【问题描述】:

我正在设计一个 API 以在 Node.js 上使用 aws-sdk 连接到 AWS ec2。我收到了来自 AWS ec2 的响应。但是,我无法使用 response.send 发送收到的响应。请帮帮我。

以下是我设计的API。

app.get("/getMachineList",function(req,res){
	console.log('getManageLabList API......')
  var params = {
  DryRun: false,
  Filters: [
    {
      Name: 'tag:Name',
      Values: [
        'abcd',
        /* more items */
      ]
    },{
      Name: 'instance-state-name',
      Values: [
        'running',
        /* more items */
      ]
    }
    /* more items */
  ]
  
};

ec2 = new AWS.EC2({ 
	accessKeyId: AWS_ACCESS_KEY_ID,
	secretAccessKey: AWS_SECRET_ACCESS_KEY,
	apiVersion: '2016-11-15'});
	ec2.describeInstances( params, function(err, data, response) {
	if (err) {
		console.log("Error", err.stack);
	} else {
		console.log("Success", JSON.stringify(data));
		response.send({
			message: data
		});
	}
	});


	
});

这就是我遇到的错误。

TypeError: Cannot read property 'send' of undefined

【问题讨论】:

    标签: javascript node.js amazon-web-services amazon-ec2 aws-sdk


    【解决方案1】:

    您应该从describeInstances 回调中删除第三个参数(响应),然后更改

    response.send({
        message: data
    });
    

    到:

    res.send({
        message: data
    });
    

    【讨论】:

    • @Itamajs 这解决了 TypeError。但是,我不确定是否发送了响应,因为我在记录响应时什么也看不到。这是我正在使用的 HTTP 请求……axios.get('http://localhost:3000/getMachineList') .then(response =>{ return response console.log(response); })
    • 您的console.log 调用无法访问,因为它位于return 语句之后。请放在return之前
    • 糟糕,我的错。我忽略了那个指示!谢谢@Itamajs
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 2020-06-17
    • 2021-11-02
    • 2021-04-12
    • 2021-06-27
    • 2021-04-17
    • 2020-08-10
    相关资源
    最近更新 更多