[英]Why am I getting the error “UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”?
I have an index.js, gettext.js and extract.js.我有一个 index.js、gettext.js 和 extract.js。
index.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获取文本.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 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
I am sending the right json data.我正在发送正确的 json 数据。 I have checked that req.body['file_name'] is accessible.
我检查了 req.body['file_name'] 是否可以访问。 The problem seems to be related to the fact that the docExtract function return a promise but I just can't seem to understand what do I do.
问题似乎与 docExtract function 返回 promise 的事实有关,但我似乎无法理解我该怎么做。 Any help would be greatly appreciated.
任何帮助将不胜感激。
The error message reads:错误消息如下:
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)
Cannot read property 'forEach'...
无法读取属性“forEach”...
The error is saying it's trying to read a property forEach
.错误是说它正在尝试读取属性
forEach
。 That means this expression:这意味着这个表达式:
blocks.forEach
...of undefined
...未定义的
...which means that blocks
is undefined. ...这意味着
blocks
是未定义的。 That means when you assign the value:这意味着当您分配值时:
const blocks = data['Blocks']
... data['Blocks']
has the value undefined
. ...
data['Blocks']
的值为undefined
。
Because getData
is an async function
, it returns a Promise
.因为
getData
是一个async function
,所以它返回一个Promise
。 If the function throws an error, and it does (trying to read forEach
property of an undefined
value), the promise becomes a rejection.如果 function 抛出错误,并且它确实(尝试读取
undefined
值的forEach
属性),则 promise 将变为拒绝。 "UnhandledPromiseRejectionWarning" means that the caller of getData
is not handling these rejections. “UnhandledPromiseRejectionWarning”表示
getData
的调用者没有处理这些拒绝。 If you wrap the await
in a try/catch
, you can catch any error that getData
throws and do something useful with it, like sending an HTTP error response.如果将
await
包装在try/catch
中,则可以捕获getData
抛出的任何错误并对其执行一些有用的操作,例如发送 HTTP 错误响应。
So hopefully this explains what the error '“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”' means.因此,希望这可以解释错误“UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'forEach'”的含义。
So to avoid the error:所以为了避免错误:
getData()
or any other async function you await the result and catch errors.getData()
或任何其他异步 function 时等待结果并捕获错误。 If you do not, then any errors, expected or unexpected, will be unhandled.data['Blocks']
is undefined, so you're either not getting the data you're expecting or you're accessing it incorrectly.data['Blocks']
是未定义的,因此您要么没有获得预期的数据,要么访问不正确。 Try logging data
to see what you're actually getting.data
以查看您实际得到的结果。 By the way, data.Blocks
is equivalent and less typing than data['Blocks']
.data.Blocks
与data['Blocks']
等效且键入更少。 I'm not a js expert but in my experience, UnhandledPromiseRejectionWarning
in this case with await
occur because await
is the short version of promise handling, but only the resolve
part.我不是 js 专家,但根据我的经验,
UnhandledPromiseRejectionWarning
在这种情况下会发生await
,因为await
是 promise 处理的简短版本,但只是resolve
部分。
ie If some exceptions are thrown inside the promise function or if the promise reject
s, then await
is not handling that.即,如果在 promise function 中抛出一些异常,或者如果 promise
reject
s,则await
不会处理。
My educated guess is that textract.analyzeDocument()
's callback (err, data) => {}
part is simply rejecting it.我有根据的猜测是
textract.analyzeDocument()
的回调(err, data) => {}
部分只是拒绝它。
It was a very silly mistake that was leading to the error.这是一个非常愚蠢的错误导致了错误。 The bucket_name variable was getting substituted from an environment variable and the variable name in gettext.js and environment file were different.
bucket_name 变量被环境变量替换,gettext.js 和环境文件中的变量名不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.