【问题标题】:AWS Lambda TypeError: callback is not a functionAWS Lambda TypeError:回调不是函数
【发布时间】:2018-04-08 05:26:03
【问题描述】:

我正在尝试在 AWS lambda 中创建 twilio 访问令牌,但我收到错误“回调不是函数”。我该如何解决?

const AccessToken = require('twilio').jwt.AccessToken;
    const VoiceGrant = AccessToken.VoiceGrant;

    exports.generateToken = function(identity, callback) {
        // Used when generating any kind of tokens
        const accountSid = 'xxxxxxxxx';
        const apiKey = 'xxxxx';
        const apiSecret = 'xxx';

        // Used specifically for creating Voice tokens
        const pushCredSid = 'xxx';
        const outgoingApplicationSid = 'xxxxx';

        // Create an access token which we will sign and return to the client,
        // containing the grant we just created
        const voiceGrant = new VoiceGrant({
            outgoingApplicationSid: outgoingApplicationSid,
            pushCredentialSid: pushCredSid
        });

        // Create an access token which we will sign and return to the client,
        // containing the grant we just created
        const token = new AccessToken(accountSid, apiKey, apiSecret);
        token.addGrant(voiceGrant);
        token.identity = identity;
        console.log('Token:' + token.toJwt());
        callback(null, token.toJwt());
    };

【问题讨论】:

  • 嘿,这条线callback(null, token.toJwt());? mh 不是你如何称呼generateToken 的重要部分吗?
  • @RolandStarke 这是一个重要的部分,因为这里应该返回结果,我做了另外两个 lambda 函数并且它们运行良好,但是在这里我不断收到我上面写的错误。
  • 嗯,AWS Lambda 是否要求您拥有 3 个参数?来自docsexports.myHandler = function(event, context, callback) {
  • @RolandStarke 不,不一定是三个参数
  • 我的意思是如果你想使用回调,你需要使用 3 个参数。 (但是是的,ofc 参数名称可能有一些魔力。但一般来说,javascript 中没有命名参数,所以我会尝试用 3 个参数定义你的函数)

标签: node.js aws-lambda twilio


【解决方案1】:

正如 Roland Starke 所说,将 exports.generateToken = function(identity, callback) 更改为 exports.generateToken = function(event, context, callback) 是值得的,一切都会正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-28
    • 2016-08-07
    • 2018-01-22
    • 2018-06-05
    • 1970-01-01
    • 2017-10-14
    • 2020-12-23
    相关资源
    最近更新 更多