我正在尝试更新DynamoDB中的字段,因此我编写了一个函数,用于根据密钥更新字段。但是,当我在API Gateway中进行测试时,俗语无法读取未定义的address
。这是我尝试测试时发回的请求正文。
{
"errorType": "TypeError",
"errorMessage": "Cannot read property 'address' of undefined",
"trace": [
"TypeError: Cannot read property 'address' of undefined",
" at Runtime.exports.handler (/var/task/index.js:6:36)",
" at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)",
" at process._tickCallback (internal/process/next_tick.js:68:7)"
]
}
以下是日志:
Execution log for request 1ff44451-a4f7-11e9-9424-bdc7992540fc
Fri Jul 12 22:48:11 UTC 2019 : Starting execution for request: 1ff44451-a4f7-11e9-9424-bdc7992540fc
Fri Jul 12 22:48:11 UTC 2019 : HTTP Method: GET, Resource Path: /update-lease/test
Fri Jul 12 22:48:11 UTC 2019 : Method request path: {address=test}
Fri Jul 12 22:48:11 UTC 2019 : Method request query string: {}
Fri Jul 12 22:48:11 UTC 2019 : Method request headers: {}
Fri Jul 12 22:48:11 UTC 2019 : Method request body before transformations:
Fri Jul 12 22:48:11 UTC 2019 : Endpoint request URI: https://lambda.us-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-1:931121055930:function:getUsersFromLease/invocations
Fri Jul 12 22:48:11 UTC 2019 : Endpoint request headers: {x-amzn-lambda-integration-tag=1ff44451-a4f7-11e9-9424-bdc7992540fc, Authorization=**************************************************************************************************************************************************************************************************************************************************************************************33e004, X-Amz-Date=20190712T224811Z, x-amzn-apigateway-api-id=ep5fg09vnb, X-Amz-Source-Arn=arn:aws:execute-api:us-west-1:931121055930:ep5fg09vnb/test-invoke-stage/GET/update-lease/{address}, Accept=application/json, User-Agent=AmazonAPIGateway_ep5fg09vnb, X-Amz-Security-Token=AgoJb3JpZ2luX2VjEG4aCXVzLXdlc3QtMSJHMEUCIDrKiCsY1blxDZc2I1HAJ6b77F9PfhP3BM4ZG6eYtyPHAiEAusWbSscpLZyvbCf4SZ4RZcKVR/j0J+uPID6TCuQAVk0q4wMIx///////////ARAAGgw5NjgyNDY1MTUyODEiDB2/KJupuwpLXocoHSq3A0mBhkMwccP029JIPikZKLl/9PO1vm85K5y9pogDLAhDuBic3Oo7ldv8TAgWHuS8OatAw6ljI650t3khNqDXzr/o2tVFuxb6Isd+QVNnzmap0ZWcJXeRnFxy0SGscFwLC2D9GA9l3sZv2PWUJUklQ+DIIq2zBZJkofjOGmrT [TRUNCATED]
Fri Jul 12 22:48:11 UTC 2019 : Endpoint request body after transformations:
Fri Jul 12 22:48:11 UTC 2019 : Sending request to https://lambda.us-west-1.amazonaws.com/2015-03-31/functions/arn:aws:lambda:us-west-1:931121055930:function:getUsersFromLease/invocations
Fri Jul 12 22:48:11 UTC 2019 : Received response. Status: 200, Integration latency: 697 ms
Fri Jul 12 22:48:11 UTC 2019 : Endpoint response headers: {Date=Fri, 12 Jul 2019 22:48:11 GMT, Content-Type=application/json, Content-Length=340, Connection=keep-alive, x-amzn-RequestId=6609ed6c-cecb-4822-98ff-90be240f31c3, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-5d290e2b-02ba3e27e439d9ea996a958f;sampled=0}
Fri Jul 12 22:48:11 UTC 2019 : Endpoint response body before transformations: {"errorType":"TypeError","errorMessage":"Cannot read property 'address' of undefined","trace":["TypeError: Cannot read property 'address' of undefined"," at Runtime.exports.handler (/var/task/index.js:6:36)"," at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)"," at process._tickCallback (internal/process/next_tick.js:68:7)"]}
Fri Jul 12 22:48:11 UTC 2019 : Method response body after transformations: {"errorType":"TypeError","errorMessage":"Cannot read property 'address' of undefined","trace":["TypeError: Cannot read property 'address' of undefined"," at Runtime.exports.handler (/var/task/index.js:6:36)"," at Runtime.handleOnce (/var/runtime/Runtime.js:63:25)"," at process._tickCallback (internal/process/next_tick.js:68:7)"]}
Fri Jul 12 22:48:11 UTC 2019 : Method response headers: {X-Amzn-Trace-Id=Root=1-5d290e2b-02ba3e27e439d9ea996a958f;Sampled=0, Content-Type=application/json}
Fri Jul 12 22:48:11 UTC 2019 : Successfully completed execution
Fri Jul 12 22:48:11 UTC 2019 : Method completed with status: 200
这是我正在运行的lambda函数:
const AWS = require('aws-sdk');
const dynamoDb = new AWS.DynamoDB.DocumentClient({ region: 'us-west-1' });
exports.handler = async function(e, ctx, callback) {
const address = e.pathParameters.address;
var data;
var params = {
TableName : 'leases',
Key: {
address
},
ConditionExpression: "attribute_exists(address)",
UpdateExpression: 'set #attrName = list_append(#attrName, :p)',
ExpressionAttributeNames: {
'#attrName': 'users'
},
ExpressionAttributeValues: {
':p': e.users
},
ReturnValues: "UPDATED_NEW"
};
try {
data = await dynamoDb.update(params).promise()
}
catch (err) {
console.log(err);
}
return void callback(null, {
isBase64Encoded: false,
statusCode: 200,
headers: { },
body: JSON.stringify(data)
});
}