简体   繁体   English

将变量从调用的 Lambda Function 传递到 DynamoDB

[英]Passing variable from invoked Lambda Function to DynamoDB

I'm trying to invoke a string from another lambda function.我正在尝试从另一个 lambda function 调用一个字符串。 This string should be written into an array which is then uploaded to a DynamoDB Table along with information such as the date.该字符串应写入一个数组,然后将该数组连同日期等信息一起上传到 DynamoDB 表中。

Yet when I try to get the variable called "key" in the lower part of the script where it is supposed to upload to DynamoDB, it is undefined and not written into the table.然而,当我尝试在应该上传到 DynamoDB 的脚本下部获取名为“key”的变量时,它是未定义的并且未写入表中。

I tried using the window.variable command but somehow AWS lambda didn't recognize it.我尝试使用window.variable命令,但不知何故 AWS lambda 无法识别它。 All in all I'm quite new and a bit confused on how to pass variables between different functions within in a JavaScript.总而言之,对于如何在 JavaScript 中的不同函数之间传递变量,我还是很陌生并且有点困惑。

Maybe someone can help me.也许有人可以帮助我。 Thank you!谢谢!

 var key; //define key as gloabal variable var AWS = require('aws-sdk'); //load aws-sdk AWS.config.region = 'eu-central-1'; //Set server region var lambda = new AWS.Lambda(); var db = new AWS.DynamoDB.DocumentClient({region: 'eu-central-1'}); //region of dynamo server exports.handler = function(event, context) { var params = { FunctionName: 'string_Generator', // called Function InvocationType: 'RequestResponse', }; lambda.invoke(params, function(err, data) { if (err) { context.fail(err); } else { context.succeed(data.Payload); } key = data.Payload; //set the payload/string as "key" variable }); } exports.handler = async (event) => { var params = { Item: { date: Date.now(), Key: key }, TableName: 'Productkeys' }; params.Item.partitionkey = "3"; //putting in the partition key of the DynamoServer return await db.put(params).promise(); }

One problem is you are overwriting your handler function, because you have 2 functions with the same name.一个问题是您正在覆盖您的handler function,因为您有 2 个同名的函数。 The other thing is it looks like this might be better in one function, as one depends on the other and I don't know how you are calling them or from where.另一件事是看起来这在 function 中可能会更好,因为一个取决于另一个,我不知道您如何称呼它们或从哪里称呼它们。

Try this:尝试这个:

exports.handler = async () => {
    var params = {
        FunctionName: 'string_Generator', // called Function
        InvocationType: 'RequestResponse',
    };
    
    lambda.invoke(params, function(err, data) {
        if (err) {
            context.fail(err);
        } else {
            context.succeed(data.Payload);  
        }

        var params2 = {
            Item: {
                date: Date.now(),
                Key: data.Payload
            },
            TableName: 'Productkeys'
        };
        params2.Item.partitionkey = "3";
        return await db.put(params2).promise();
    });
};

Okay I went through it the whole night but I was able to fix it with @ffritz advice.好的,我整晚都经历了它,但我能够通过@ffritz 的建议来修复它。

I tried some different combinations and tried to understand the syntax a lot better until it finally worked out.我尝试了一些不同的组合,并试图更好地理解语法,直到最终成功。 It might not be the prettiest code but it works .它可能不是最漂亮的代码,但它可以工作

Thank you for your help guys.谢谢你们的帮助。

Edit: I found one more issue.编辑:我又发现了一个问题。 The code doesnt get executed immediately.代码不会立即执行。 The generated keys just stay in the function and are deployed upon the next call.生成的密钥仅保留在 function 中,并在下一次调用时部署。 So when I'm calling the function via API Gateway, I dont deploy the freshly generated string, but the old one.因此,当我通过 API 网关调用 function 时,我不部署新生成的字符串,而是部署旧字符串。 I still need to find a fix for that.我仍然需要为此找到解决方法。

Edit 2: Now it works.编辑2:现在可以了。 I updated the script below.我更新了下面的脚本。

 var AWS = require('aws-sdk'); //load aws sdk AWS.config.region = 'eu-central-1'; //Server of Lambda var lambda = new AWS.Lambda(); var db = new AWS.DynamoDB.DocumentClient({region: 'eu-central-1'}); //region of DB Server exports.handler = function(event, context, callback) { var params = { //set parameters for the call FunctionName: 'String_Generator', // called Function InvocationType: 'RequestResponse', }; lambda.invoke(params, function(err, data) { //Invoke other function if (err) { context.fail(err); } else { //if invoke gets string var params2 = { //set parameters for DB put Item: { date: Date.now(), Key: data.Payload //set the string as "Key" variable }, TableName: 'Productkeys' }; params2.Item.partitionkey = data.Payload; //add the partition key db.put(params2); //put the params2 into the DB callback(null, data.Payload); } }); };

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

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