javascript - AWS Lambda 和 Node.js 函数未被调用

标签 javascript node.js aws-lambda hubspot

我对 AWS lambda 和 node.js 都很陌生。我已经研究过一些异步架构,因此我对回调很熟悉,但仍然缺乏经验。我确信这在某种程度上是相关的,但我不明白为什么,因为如果我在本地测试它,它会完美运行。我已正确设置环境变量并测试它是否也已通过。

在下面的代码片段中,我使用 Claudia Bot Builder 来获取 slack-slash-command 消息,并将其传递给此 SlackResponse 函数,该函数使用 Node Hubspot API 按用户名进行查询。由于某种原因,client.contacts 搜索命令永远不会被调用。所有 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;

}

谁能解释一下为什么会发生这种情况?这是权限问题吗?为什么它在本地运行而不是在 AWS 中运行?

最佳答案

这似乎是 JavaScript promise 问题。

Claudia API 和 Bot Builders 使用 JavaScript Promise 来处理异步操作。如果您在本地运行此示例,它将起作用,因为您的函数将执行,但是如果您在 Lambda 函数上运行它 client.contacts.search(SenderID,) 将破坏 promise 链,并且 Lambda 函数将被关闭,这意味着不会执行任何其他操作。

要解决这个问题,如果 JavaScript 不支持开箱即用的异步操作,您需要将异步操作包装到 JavaScript promise 中。例如:

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

应该变成:

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

或者,在您的示例中:

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

    // Do your logic
    resolve(finalResult)
  }
}

关于javascript - AWS Lambda 和 Node.js 函数未被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44606992/

相关文章:

javascript - 在 HTML/CSS 中实现 Node Js

node.js - node.js 服务器的 SSL 握手失败

amazon-web-services - CloudFormation Lambda S3 存储桶访问被拒绝

reactjs - 尝试使用 Node.js Lambda 函数在 AWS REST API 配置上通过 Axios 发布数据时出现 CORS 错误

javascript - 如何将 ids 数组与包含其 id 作为属性名称一部分的对象属性进行匹配

javascript - 获取属性访问权限被拒绝 "document"

javascript - JQuery - 显示/隐藏样式属性覆盖 css

javascript 模板 chop 一些数据

node.js - 如何在 Meteor 应用程序中 "break up"长时间运行的服务器端函数?

amazon-web-services - 如何使用缓存的 boto3 客户端和 Lambda 预置并发刷新凭证?