编程语言
首页 > 编程语言> > JavaScript-传递AWS Lambda函数的查询参数

JavaScript-传递AWS Lambda函数的查询参数

作者:互联网

我正在尝试设置一个Lambda函数,该函数将提取传递到创建的API网关URL中的查询参数. (侧边栏:在编程方面,我还是很环保的,所以请原谅我对如何命名事物的任何无意的困惑).我已经使用Synchronize.JS在光纤中包装了一些REST调用,当我对要传递到各个REST URL中的变量进行硬编码时,它的工作效果很好,但是我们的目标是能够传递将充当进行不同的REST调用的全局变量.这就是我目前拥有的…

// Dependendies
var request = require('superagent');
var sync = require('synchronize');


exports.handler = function(event, context) {

    sync.fiber(function() {
        var followingArray = [];
        var followersArray = [];
        var postsCountArray = [];
        var allSqorCountArray = [];
        var trendingPostAuthorArray = [];
        var hashtagCountArray = [];
        var notificationCountArray = [];

        var getParam = function(query){
                var SearchString = window.location.search.substring(1);
                var VariableArray = SearchString.split('&');
                for(var i = 0; i < VariableArray.length; i++){
                var KeyValuePair = VariableArray[i].split('=');
                if(KeyValuePair[0] == query){
                    return KeyValuePair[1];
                }
                };
        };

        var userId = getParam('userId');
        var limit = getParam('limit');
        var offset = getParam('offset');

        var restCallOne = function() {
            request('https://someurl.com/etc/' + userId + '/follows?limit=' + limit + '&offset=' + offset)
            .end(function(err, res) {
                if (err) {
                    console.log('Request 1');
                    context.fail("Danger Will Robinson!!! Follows Count Does Not Like You!!");
                } else {
                    var followsCount = res.body.total_count;
                    followingArray.push(followsCount);
                }
            });
        };

        var restCallTwo = function() {
            request
            .get('https://someurl.com/etc/etc/etc?limit=' + limit + '&offset=' + offset)
            .set({'access-token': 'someAccessToken'})
            .end(function(err, res) {
                if(err) {
                    console.log('Request 4');
                    context.fail("Danger Will Robinson!!  The All Sqor Feed is moody");
                } else {
                    var allSqorCount = res.body.total_count;
                    allSqorCountArray.push(allSqorCount);
                }
            });
        };

        var restCallThree = function() {
            request
            .get('https://someUrl.com/etc/' + userId + '/followers?limit=' + limit + '&offset=' + offset)
            .end(function(err, res) {
                if (err) {
                    console.log('Request 3');
                    context.fail("Danger Will Robinson!!! Following Count Done Smacked You Upside The Head!!");
                } else {
                    var count = res.body.total_count;
                    followersArray.push(count);
                    context.done(null, 'The total number of people that follow Andy is ' + followersArray[0] +
                                       ', and the number of people that Andy follows is ' + followingArray[0] + 
                                       ', and the number of posts in his feed is ' + allSqorCountArray[0]);
                }
            });
        };

        try {
            restCallOne(userId, limit, offset);
        } catch(errOne) {
            console.log("Error on call 1!!");
        }

        try {
            restCallTwo(limit, offset);
        } catch(errTwo) {
            console.log("Error on call 2!!");
        }

        try {
            restCallThree(userId, limit, offset);
        } catch(errThree) {
            console.log("Error on call 3!!");
        }
    });
};

当我将此函数链接时生成的API网关URL如下:https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams

但我希望能够传递这样的信息,并在Lambda函数中使用params从其余的调用中返回正确的信息:
https://someID.execute-api.us-east-1.amazonaws.com/syncFunctionalParams?userId=212733&limit=25&offset=0

这可能吗??我会偏离这个方向吗?

解决方法:

您需要在API网关中设置mapping template.如果您提前知道参数的名称,则模板可能如下所示:

{
  "userId": "$input.params('userId')",
  "limit": "$input.params('limit')",
  "offset": "$input.params('offset')"
}

当事件传递给Lambda时,将评估每个$input.params(‘…’)的位置,并将查询字符串中的值放在其位置.

如果您不提前知道参数名称,则必须在Lambda中进行一些解析.您的映射模板如下所示:

{
  "querystring": "$input.params().querystring"
}

在传递给Lambda的事件中,最终看起来像这样:

{
  "querystring": "{limit=25, offset=0, userId=212733}"
}

然后,您将解析getParam()中的event.querystring而不是window.location.search.显然,您将需要更改一些逻辑,因为您将使用逗号而不是&符进行分隔,并且需要除去花括号.顺便说一下,由于您目前在服务器上,因此您没有窗口对象.

标签:aws-lambda,aws-api-gateway,javascript
来源: https://codeday.me/bug/20191119/2035533.html