简体   繁体   English

DynamoDB.putItem 未保存

[英]DynamoDB.putItem is not saving

this code is called after Cognito signup postConfirmation, the dynamo.putItem is not working and is not logging to Cloudwatch.此代码在 Cognito 注册 postConfirmation 后调用,dynamo.putItem 不工作且未登录到 Cloudwatch。 I've tried to add CloudTrail logs but they are not showing anything.我尝试添加 CloudTrail 日志,但它们没有显示任何内容。

What is the reason ?是什么原因 ?

Thanks for your help !谢谢你的帮助 !

...

module.exports.router = async (event, context) => {
    if (process.env.npm_lifecycle_event !== "test") {
        console.debug("event", event);
        console.debug("context", context);
    }
    let response = null;
 
    if (event.userPoolId) {
        if (event.userPoolId == process.env.COGNITO_POOL) {
            try {
                
                if (event.triggerSource == 'PostConfirmation_ConfirmSignUp') {
 
                    const now = new Date().toISOString();
                    var actual = {
                        userName: event.userName,
                        created: now,
                        publicKey: "la mia chiave pubblica",
                        privateKey: "la mia chiave privata"
                    };
 
                    dynamo.putItem({
                        ReturnConsumedCapacity: "TOTAL",
                        TableName: process.env.USERS_TABLE,
                        Item: converter.marshall(actual)
                    }, (_err, _data) => {
                        console.debug("into putItem !!!");
                         
                        if (_err) {
                            console.log("error", _err);
                        } else {
                            console.log("added", _data);
                        }
                    });
 
                } else {
                     
                }                               
            } catch (error) {
                console.log(error);
            }           
            response = event;
        } else {
            throw new Error(`Wrong pool event.userPoolId`);
        }
    } else {
        throw new Error(`No pool`);
    }
    return response; 
}

You are not waiting for dynamo.putItem to complete, the Lambda function is shutting down before a network request is made.您不是在等待 dynamo.putItem 完成,而是在发出网络请求之前关闭 Lambda 函数。

Since you have set your function to be async, you should call .promise() on the putItem() method so that it returns a promise.由于您已将函数设置为异步,因此您应该在 putItem() 方法上调用 .promise() 以便它返回一个 Promise。 you can then await the promise so that the lambda does not stop until the DynamoDB call succeeds.然后,您可以等待承诺,以便在 DynamoDB 调用成功之前 lambda 不会停止。

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

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