简体   繁体   English

不会调用AWS Lambda和Node.js函数

[英]AWS Lambda and Node.js function not getting called

I'm fairly new to both AWS lambda and node.js. 我对AWS lambda和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. 在下面的代码片段中,我正在使用Claudia Bot Builder抓取一条slack-slash-command消息,并将其传递给此SlackResponse函数,该函数使用Node Hubspot API通过用户名进行查询。 For some reason, the client.contacts search command is never called. 由于某些原因,永远不会调用client.contacts搜索命令。 All console.log() reports show up, but the callback for the search never seems to execute, even asynchronously. 显示所有console.log()报告,但是搜索的回调似乎从未执行,即使是异步执行。

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? 为什么要在本地运行而不在AWS中运行?

This seems like a JavaScript promises issue. 这似乎是一个JavaScript promises问题。

Claudia API and Bot Builders are using JavaScript promises to handle async operations. Claudia API和Bot Builders正在使用JavaScript promises处理异步操作。 If you run this example locally it will work because your function will execute, however if you run it on Lambda function client.contacts.search(SenderID, will break promise chain and Lambda function will be closed, which means that nothing else will be executed. 如果在本地运行此示例,则它将起作用,因为您的函数将执行,但是,如果在Lambda函数client.contacts.search(SenderID,上运行它client.contacts.search(SenderID,则将中断client.contacts.search(SenderID,链,并且Lambda函数将被关闭,这意味着将不执行任何其他操作。

To solve that problem you need to wrap async operations to JavaScript promises if they don't support them out of the box. 要解决该问题,您需要将异步操作包装到JavaScript Promise(如果它们不支持)。 For example: 例如:

setTimeout(function () {
  return 'Hello'
}, 1000)

Should become: 应该变成:

return Promise(function(resolve, reject) {
  setTimeout(function () {
    resolve('Hello')
  }, 1000)
})

Or, in your example: 或者,在您的示例中:

return new Promise(function(resolve, reject) {
  client.contacts.search(SenderID, function (err, res) {
    if (err) {
      return reject(err)
    }

    // Do your logic
    resolve(finalResult)
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM