我使用带有aws-sdk的Node.Js来填充var message
。
1)首先,我调用一个函数来获取AWS Groups - 然后我将其连接到消息。
2)其次我根据第一个函数的数据调用另一个函数 - 然后我将其连接到消息。
最后我发送电子邮件并使用消息字符串。
问题:如何连接字符串message
并将其传递给函数SendEmail?
这是我的代码。
var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-1'});
var iam = new aws.IAM({apiVersion: '2010-05-08'});
exports.handler = function (event, context) {
console.log("Incoming: ", event);
var message = "";
function AwsListGroupsFunction(err, data)
{
if (err) console.log(err, err.stack); // an error occurred
else
{
//Get AWS Groups from AWS Account
var dataCount = data.Groups.length-1;
for (var key in data.Groups)
{
message+= data.Groups[key].GroupName
// Then get all users associated to the GroupName
var groupParams = { GroupName: data.Groups[key].GroupName};
iam.getGroup(groupParams, AwsGetGroupFunction); // end of else for if iam.getGroup
if (key != dataCount)
message += ', ';
}
}
return message;
};
function AwsGetGroupFunction(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else{
// successful response
message+= "<br/>";
for (var userKey in data.Users) {
message += data.Users[userKey].UserName + ' and PasswordLastUsed: ' + data.Users[userKey].PasswordLastUsed + ' , ';
}
}
return message;
};
function SendEmail(message){
console.log('===SENDING EMAIL HERE using message variable===');
}
// This is the only function called which calls other functions
var params = {};
iam.listGroups(params, AwsListGroupsFunction);
};
答案 0 :(得分:0)
AwsListGroupsFunction(错误,数据)函数用作iam.listGroups的回调。因此,您无法在此上下文中从AwsListGroupsFunction函数返回值。其他回调函数也是如此。
你可以通过多种方式解决这个问题,像mapSeries这样的异步库方法会是更好的选择。这里是一个简单解决方案的伪代码。你可以通过进一步的重构来改进它。
var aws = require('aws-sdk');
var ses = new aws.SES({region: 'us-east-1'});
var iam = new aws.IAM({apiVersion: '2010-05-08'});
var async = require('async');
exports.handler = function (event, context) {
console.log("Incoming: ", event);
var message = "";
function AwsListGroupsFunction(err, data)
{
if (err) console.log(err, err.stack); // an error occurred
else
{
//Get AWS Groups from AWS Account
var dataCount = data.Groups.length-1;
async.mapSeries(data.Groups, function(group, cb) {
var partialMessage = group.GroupName;
// Then get all users associated to the GroupName
var groupParams = { GroupName: group.GroupName};
iam.getGroup(groupParams, function(err, data) {
if (err) {
console.log(err, err.stack); // an error occurred
cb(err, "");
}
else{
// successful response
partialMessage+= "<br/>";
for (var userKey in data.Users) {
partialMessage += data.Users[userKey].UserName + ' and PasswordLastUsed: ' + data.Users[userKey].PasswordLastUsed + ' , ';
}
cb(null, partialMessage);
}
});
}, function(err, result) {
if (err) {
//handle error. Expected message is not ready
}
else {
/*
Here result contains array of partial messages; ie [partial message for group1, partial message for group2, ...]
concatinate partial messages and send email.
*/
}
});
}
};
function SendEmail(message){
console.log('===SENDING EMAIL HERE using message variable===');
}
// This is the only function called which calls other functions
var params = {};
iam.listGroups(params, AwsListGroupsFunction);
};