【发布时间】:2021-06-19 16:25:13
【问题描述】:
我有一个 index.js、gettext.js 和 extract.js。
index.js
app.post("/anaData", async function(req, res) {
const someData = await docExtract(req.body['file_name'])
const data = await getData(someData)
.....
....
}
gettext.js
async function docExtract (file_name) {
return new Promise(resolve => {
var file_params = {
Document: {
S3Object: {
Bucket: bucket_name,
Name: filename
}
},
FeatureTypes: ['TABLES'],
}
textract.analyzeDocument(file_params, (err, data) => {
if (err) {
return resolve(err)
} else {
resolve(data)
}
})
})
}
module.exports = docExtract
extract.js
async function getData(data) {
const blocks = data['Blocks']
const blocksMap = {}
const tableBlocks = []
blocks.forEach(block => { //At this point the error is generated: TypeError: Cannot read property 'forEach' of undefined"
blocksMap[block['Id']] = block
....
....
}
module.exports = getData
我正在发送正确的 json 数据。我检查了 req.body['file_name'] 是否可以访问。问题似乎与 docExtract 函数返回一个承诺有关,但我似乎无法理解我该怎么做。任何帮助将不胜感激。
错误信息如下:
UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined
at extract (path\to\getData.js:75:12)
at path\to\index.js:28:22
at processTicksAndRejections (internal/process/task_queues.js:97:5)
【问题讨论】:
-
错误来源是
blocks未定义。所以forEach循环失败了……然后,promise 也失败了。确保data['Blocks']在getData()中定义。
标签: javascript node.js amazon-web-services es6-promise amazon-textract