如何调试我的 dynamodb 插件不工作

How to debug my dynamodb insert not working

我无法在我的 dynamodb 中插入项目 table。

我的代码如下:

   function insert() {
        const item = {
            itemId: 'itemId',
            name: 'itemName',
            status: 'itemStatus'
        };

        const params = {
            TableName: 'sls-basic-operations-items-dev',
            Item: item
        };

        console.log('ready to insert', params);

        var docClient = new AWS.DynamoDB.DocumentClient();

        docClient.put(params, function (err, data) {
            if (err) {
                console.log(err);
                return false;
            } else {
                console.log(data);
                return true;
            }
        });
    }

    module.exports.handler = async (event) => {
        const message = event.Records[0].Sns.Message;
        console.log('new event to be consumed', message);

        await insert();
}

lambda 日志条目也不是很详细。

我只看到:

START RequestId: b71ea056-85f0-424d-ad55-4b4571aeeccf Version: $LATEST
2019-06-14T23:13:13.012Z    b71ea056-85f0-424d-ad55-4b4571aeeccf    new event to be consumed
{
    "description": "test item"
}

    2019-06-14T23:13:13.032Z    b71ea056-85f0-424d-ad55-4b4571aeeccf    ready to insert { TableName: 'sls-basic-operations-items-dev',
    Item: { itemId: 'itemId', name: 'itemName', status: 'itemStatus' } }
    END RequestId: b71ea056-85f0-424d-ad55-4b4571aeeccf
    REPORT RequestId: b71ea056-85f0-424d-ad55-4b4571aeeccf  Duration: 119.28 ms Billed Duration: 200 ms Memory Size: 1024 MB    Max Memory Used: 76 MB  

仅此而已,table...

中没有任何条目

有什么想法吗?

问题是 insert() 函数是作为同步函数调用实现的(当 docClient.put() 被调用时它 return 立即 undefined ),尽管有意是异步的。

尝试将 insert() 函数的最后几行更改为 return 承诺:

 return docClient.put(params).promise();

然后您可以将对 insert() 的调用包装在 try / catch:

 try {
    const data = await insert();
    console.log('success', data);
 } catch (err) {
    console.log('failure', err);
    throw err;
 }:

有关在 Lambda 函数中使用 Promiseasync / await 的更多信息,请参阅 Node.js 8.10 runtime now available in AWS Lambda