【问题标题】:I need to save dynamoDB return value into variable我需要将 dynamoDB 返回值保存到变量中
【发布时间】:2019-12-27 18:52:41
【问题描述】:

我在 AWS DynamoDB 和 Lambda 上做一些工作,我正在使用 lambda 来获取 dynamoDb 表数据。我得到了完美的结果,但我需要将该输出放到 dynamoDb 函数之外,并且我需要保存在一个变量中以供其他用途。

var AWS = require('aws-sdk');

const doClient = new AWS.DynamoDB.DocumentClient({region: 'us-east-1'});

exports.handler = function(e, ctx, callback) {

    var params = {  
        TableName: 'table1',
        Key: { 
            "Device" : "123456",
            "Date_Time" : "2019-8-21"
        }
    };

    doClient.get(params, function(err, data) {
        if (err) {
            callback(err, null);
        } else {
            console.log(data);
            console.log(data.Item.Blue);
            var blue = data.Item.Blue;
            console.log(blue);
            return blue;
        }
    });

    //console.log(blue);   // how can get that blue value to here
};

【问题讨论】:

    标签: javascript amazon-web-services asynchronous aws-lambda amazon-dynamodb


    【解决方案1】:

    javascript 中的 Promise 异步工作,这意味着您的程序在执行 console.log(blue) 之前不会等待“doClient.get”函数返回。

    所以你需要等待函数 doClient.get 返回结果 您的处理程序代码将是这样的

    var params = {  
            TableName: 'table1',
            Key: { 
                "Device" : "123456",
                "Date_Time" : "2019-8-21"
            }
        };
    
    docClient = require('util').promisify(doClient)
    
    var blue = await docClient(params);
    
    console.log(blue);

    Amazon DynamoDB DocumentClient().get() use values outside of function

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-13
      • 2011-11-11
      • 1970-01-01
      • 2013-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-25
      相关资源
      最近更新 更多