I am trying to put item to the local dynamodb. I tried logging to check where the issue was, and one of the issue is, or probably is that the then
method is being executed before the preceding method completes its execution. Below is the Controller
class code which calls the addCompany method in another class
1: registerCompany() method from the Controller class
public async registerCompany(data, TableName) : Promise <HttpResponse>{
return new Promise((resolve, reject) => {
this.registerCompanyService = new RegisterCompanyService();
let companydata = this.registerCompanyService.addCompany(data,TableName).then(result =>{ //then is being executed before the addCompany finishes its execution
if(!result){
console.log("All data not provided")
return reject(this.createBadRequestResponse())
}
console.log(`The type of return data is ${typeof result}`)
resolve();
});
if(companydata!=null)
{
console.log(`The company data is ------> ${companydata}`)
return this.createSuccessResponse(companydata);
}
else {
return this.createInternalServerErrorResponse();
}
})
}
2: addCompany() method from the CompanyService class
public async addCompany(companyData: Company, TableName): Promise < any > {
this.companyDao = new CompanyDao();
var params = {
TableName,
Item: {
key1: companyData.key1,
key2: companyData.key2,
key3: companyData.key3,
}
}
console.log(`The params stringified before sending to dao are -------> ${JSON.stringify(params)}`);
this.companyDao.addCompany(params);
}
3: addCompany() methodfrom the DAO class inserting data into DB
public async addCompany(params): Promise < any > {
console.log(`Inside DAO params-------> ${params.item}`);
let putItem = new Promise < any > ((resolve, reject) => {
this.dynamodbClient.put(params, (error, result) => {
console.log(`Inside put params --------> ${params.item}`)
if (error) {
console.error(`Unable to add item. Error ---> ${JSON.stringify(error)}`)
reject(error);
} else {
console.log(`Added Item -----> ${params.Item}`)
resolve(true)
}
})
})
const result = await putItem;
console.log(`This is the final result -------->${result}`);
return result;