1

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;
Gaurav Thantry
  • 103
  • 1
  • 13

1 Answers1

1

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)}`);

    //When calling a promise you either return it or wait for its completion using await.
    await this.companyDao.addCompany(params);
    //OR return it
    return this.companyDao.addCompany(params);
}

You're actually making an asynchronous call but you're not the returning the Promise from the DAO class to the controller. So there is actually no means for the calling method to know if addCompany from DAO has resolved. Also, avoid using same method names even if they are in different classes/services.

I'm not 100% certain about your addCompany(DAO) method. Good luck ;)

CrazyYoshi
  • 1,003
  • 1
  • 8
  • 19
  • Thank you. Your solution worked for that class. I am looking into DAO now. Figuring out why it is not waiting for the `put` method to complete its execution before returning to the `Controller` class. – Gaurav Thantry Sep 20 '20 at 22:55
  • You can convert your put method to a promise, and should return it directly unless you have some tweaks to do on your data. Don't forget to mention `ReturnValues`. I'm not very familiar with aws, but I think your last problem could be resolved with this SO question : [Put object and promise](https://stackoverflow.com/questions/55166921/dynamodb-put-promise-not-returning-the-put-object) – CrazyYoshi Sep 20 '20 at 23:19
  • Excellent! Worked like a charm!!! Thanks a lot. Converting `put` to a `promise` worked – Gaurav Thantry Sep 20 '20 at 23:32