Typescript 运行时错误“TypeError:无法读取未定义的”-->aws s3 selectObjectContent和dynamodb putItem期间的属性“Payload”
您好,我正在编写一个基于类型脚本的异步函数aws lambda函数,以使用s3.selectObjectContent处理aws s3中的数据,并在使用dyanmodb.putItem时将提取的值写入dynamodb中的表。当处理s3数据时,我会遇到一个错误,但是异步调用函数时,仍然会完成对数据库表的写入。出于某种原因,在s3命令完成并返回承诺之前,存储s3有效负载数据的变量被报告为未定义。你能帮我想出一个更好的方法来编写这段代码,或者建议我如何修复它吗Typescript 运行时错误“TypeError:无法读取未定义的”-->aws s3 selectObjectContent和dynamodb putItem期间的属性“Payload”,typescript,asynchronous,amazon-s3,aws-lambda,amazon-dynamodb,Typescript,Asynchronous,Amazon S3,Aws Lambda,Amazon Dynamodb,您好,我正在编写一个基于类型脚本的异步函数aws lambda函数,以使用s3.selectObjectContent处理aws s3中的数据,并在使用dyanmodb.putItem时将提取的值写入dynamodb中的表。当处理s3数据时,我会遇到一个错误,但是异步调用函数时,仍然会完成对数据库表的写入。出于某种原因,在s3命令完成并返回承诺之前,存储s3有效负载数据的变量被报告为未定义。你能帮我想出一个更好的方法来编写这段代码,或者建议我如何修复它吗 export class Handler
export class Handler {
import { S3, DynamoDB } from 'aws-sdk'
import { DID_TABLENAME, IP_DEVICE_HISTORY_TABLENAME } from './Constants'
dynamodb = new DynamoDB()
s3 = new S3()
async handle(event: SQSEvent): Promise<void> {
console.log('Entering handler')
console.log(event)
this.srcBucket = 'my-bucket'
this.srcKey = 'myfile.gzip'
await this.prepareAndQuery()
}
private async prepareAndQuery(): Promise<any> {
console.log('Preparing params')
const params = {
Bucket: this.srcBucket,
Key: this.srcKey,
ExpressionType: 'SQL',
Expression: 'SELECT * FROM s3object',
InputSerialization: { CompressionType: 'GZIP', JSON: { Type: 'LINES' }},
OutputSerialization: { JSON: { RecordDelimiter: ' ' }}}
console.log('Sending s3 command')
try {
this.s3selectOutput = await this.s3.selectObjectContent(params).promise()
const events: any = this.s3selectOutput.Payload
for await (const event of events) {
try {
if(event.Records) {
// THIS LINE BELOW IS THE POINT OF FAILURE
const s3selectOutput: any = event.Records.Payload.toString()
const decoded = decodeURIComponent(s3selectOutput.replace(/\+|\t/g, ' '))
const jsoncontent = JSON.parse(decoded)
const udid = jsoncontent.pyAppResponse.logs.udid_logs.udid;
const did = jsoncontent.pyAppResponse.logs.udid_logs.did;
if ((did !== null) && (udid !== null)) {
events.pause()
const paramsdiddB = {
Item: {
'did': { S: did },
'udid': { S: udid }},
ReturnConsumedCapacity: 'TOTAL',
TableName: DID_TABLENAME }
console.log(`going to write to dbs ${DID_TABLENAME}`)
const _retValue = await this.dynamodb.putItem(paramsdiddB).promise().then((_dBdata: any) => {
console.log(`Successfuly wrote to tables: ${DID_TABLENAME}`)
}).catch((dBerr: any) => {
console.log('Error writing to database tables')
console.log(dBerr.stack)
})
events.resume()
}
}
}
catch (s3undefinederr) {
if (s3undefinederr instanceof TypeError) {
console.log(s3undefinederr)
throw s3undefinederr
}
}
if (event.End !== null) {
console.log('SelectObjectContent completed')
}
}
}
catch (s3error) {
console.log(s3error)
throw s3error
}
return this.s3selectOutput
}
}
运行时出现错误消息,即使它已完成作业
at Handler.<anonymous> (/var/task/build/dist/Handler.js:144:66)
at Generator.next (<anonymous>)
at fulfilled (/var/task/build/dist/Handler.js:5:58)
at processTicksAndRejections (internal/process/task_queues.js:97:5)
2020-09-30T04:58:57.312Z 9783b6c9-d88e-4b14-82d0-607bd14ec730 ERROR Invoke Error {"errorType":"TypeError","errorMessage":"Cannot read property 'Payload' of undefined","stack":["TypeError: Cannot read property 'Payload' of undefined"," at Handler.<anonymous> (/var/task/build/dist/Handler.js:144:66)"," at Generator.next (<anonymous>)"," at fulfilled (/var/task/build/dist/Handler.js:5:58)"," at processTicksAndRejections (internal/process/task_queues.js:97:5)"]}```
我可以通过使用如下的try+catch语句捕获错误来修复此错误
catch (s3undefinederr) {
console.log(s3undefinederr)
if (s3undefinederr instanceof Error) {
throw s3undefinederr
}
}