I'm fairly new to both AWS lambda and node.js. I've wrestled through some of the asynchronous architecture such that I am familiar, but still inexperienced, with callbacks. I'm sure that this is related somehow, but I do not understand why, because it runs perfectly if I test it locally. I have set up the environment variable correctly and tested that it is being passed too.
In the code snippet below, I am using Claudia Bot Builder to grab a slack-slash-command message, passing it to this SlackResponse function, which is using the Node Hubspot API to make a query by username. For some reason, the client.contacts search command is never called. All console.log() reports show up, but the callback for the search never seems to execute, even asynchronously.
function SlackResponse(message, request, debug, currentresponse){
var SlackResponse = currentresponse;
var SenderID = message.originalRequest.user_name;
var CustomerID = 0;
if (debug){
SlackResponse += 'Slack Debug On' + '\r';
SlackResponse += 'SenderID: ' + SenderID + '\r';
}
client.useKey(process.env.HAPI_Key);
console.log('API Key set \r');
console.log(message);
console.log(currentresponse);
client.contacts.search(SenderID,
function processSearchResult(err, res) {
console.log('In processSearchResult function \r');
console.log(JSON.stringify(res));
if (err) { console.log('uh oh'); throw err; }
if (res.total === 0)
{
if(debug){console.log('New Customer Identified' + '\r')}
// Create a new Contact
var payload = {
"properties": [
{
"property": "firstname",
"value": SenderID
}]
}
client.contacts.create(payload, function(err, res){
if (err) { throw err; }
CustomerID = res.vid;
if(debug){console.log('New Customer Created with CustomerID: ' + CustomerID + '\r')}
})
}
else
{
CustomerID = res.contacts[0].vid;
if(debug){console.log('Hubspot CustomerID:' + CustomerID + '\r')}
}
}
);
console.log('About to return \r');
return SlackResponse;
}
Can anyone explain why this is occurring? Is this a permissions issue? Why does this run locally but not in AWS?