I'm new to Angular and Node and have also been playing about with AWS recently. I like the concept of serverless computing so am attempting to write an AngularJS app which will invoke Node.JS microservices hosted on AWS Lambda.
I've got as far as invoking my Lambda function through an Angular factory and getting a successful response. I can output the Lambda payload I've defined in my Lambda function to the console from within the Angular factory after calling the lambda.invoke() method, but I can't get the data into my controller to actually use it. I just get an "undefined" error.
I've been reading around it for hours, on this site and others and my inexperience with Angular is making this very hard to work through myself, so any help is appreciated. I feel like the answer must be some variation on this (angular controller is executing before factory complete) .......but I just can't get it working
The factory is below:
my.factory("AWSbits", function() {
var params = {
FunctionName: 'arn:aws:lambda:eu-west-1:xyz',
InvocationType: 'RequestResponse',
LogType: 'Tail',
Payload: JSON.stringify ( {
".....some stuff"
}),
};
var lambda = new AWS.Lambda();
return{
lambdaexecute : function(){
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
console.log(data); // successful response
if (data.Payload != '{}') {
jdata = JSON.parse(data.Payload);
return jdata;
}
}
})}};
);
The controller is:
my.controller('AWSController', [
'$scope',
'AWSbits',
function($scope, AWSbits){
$scope.output = AWSbits.lambdaexecute();
console.log($scope.output); ====> undefined
}
]);