I am working on something dynamo needs to trigger an API once the records are successfully inserted. It is inserting the records but doesn’t log the success message or call the API. Once I remove the API call from the condition, everything works fine as in success message is logged and api is also triggered. Code below –
const sendTrigger = async (apikey) => {
try {
logger.info('Initiating trigger')
const res = await axios.post(`/abc/xyz`,{}, {
headers: {
'apikey': apikey
}
})
console.log('Response', res)
return res
} catch (err) {
logger.error('Error in sending ', err)
throw err
}
}
dynamo.put(params, async (err, data) => {
if (err) {
logger.error('Unable to add item. Error JSON:', JSON.stringify(err, null, 2));
} else {
console.log('Insert dynamoDB successful');
try {
const response = await sendTrigger(apiKey);
logger.info('Response', response);
} catch (error) {
logger.error('Error occurred while sending trigger:', error);
}
}
});
If I do this it works fine –
dynamo.put(params, (err, data) => {
if (err) {
logger.error('Unable to add item. Error JSON:', JSON.stringify(err, null, 2));
} else {
console.log('Insert dynamoDB successful');
}
});
const response = await sendTrigger(apiKey);
logger.info('Response', response);
But my condition is like the trigger should be called only when the records are inserted. I am assuming it has something to do with async/await but not able to figure out. Any suggestions?