Currently, I am utilizing a Lambda function to retrieve data from a database and would like to ensure that the page rendering process is delayed until the data has been fetched.
I am aware that there is a way to achieve this using a combination of promises and *ngIf, as explained here. However, I am unsure of how to integrate this solution into my existing code.
Here is a snippet of my code:
overview.component.ts
export class OverviewComponent implements OnInit {
businessPartner: any;
bpNumber: number;
paymentDetail: PaymentDetails;
response: any;
constructor() { }
ngOnInit() {
AWS.config.update({
//Testuser
accessKeyId: "XXXXXXXXXXXXX",
secretAccessKey: "XXXXXXXXXXXXXXXXXX",
region: "eu-west-1",
});
//get bpNumber from session storage
this.bpNumber = +JSON.parse(sessionStorage.getItem('bpNumber'));
this.lambdaGetBp(this.bpNumber);
};
lambdaGetBp(bpNumber: number): any{
var lambda = new AWS.Lambda();
var params = {
FunctionName: 'readFromDynamoDb', /* required */
Payload: JSON.stringify({
bpNumber : bpNumber,
})
};
lambda.invoke(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else {
var jsonObj = JSON.parse(data.Payload)
console.log(jsonObj) // successful response
return jsonObj;
}
});
}
click(){
console.log(this.businessPartner);
}
}
And here's my Lambda function in index.js
'use strict';
var AWS = require('aws-sdk');
var docClient = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-1' });
exports.handler = function(e, ctx, callback) {
let table = "dsbTable";
let bpNumber = e.bpNumber;
let params = {
TableName: table,
Key: {
"bpNumber": bpNumber
},
};
docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
callback(err, null);
} else {
var xml2js = require('xml2js'); // XML2JS Module
var parser = new xml2js.Parser();
parser.parseString(data.Item.getBp, function(err, result) {
if (err) {
console.log('Error!!!!!!')
} else {
data = result;
console.log('Done');
}
});
};
callback(null, data);
});
};
Your assistance with this matter is greatly appreciated :)