【发布时间】:2021-01-06 19:01:50
【问题描述】:
我正在尝试将项目放入本地 dynamodb。我尝试记录以检查问题出在哪里,其中一个问题是,或者可能是在前面的方法完成执行之前正在执行 then 方法。下面是调用addCompany method in another class的Controller类代码
1:Controller 类中的 registerCompany() 方法
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:CompanyService 类中的 addCompany() 方法
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:DAO 类中的 addCompany() 方法将数据插入 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;
【问题讨论】:
标签: javascript node.js lambda types amazon-dynamodb