简体   繁体   English

为什么我收到错误“UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined”?

[英]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:所以为了避免错误:

  1. To avoid unhandled rejections, make sure when you call 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.如果您不这样做,那么任何预期或意外的错误都将无法处理。
  2. To avoid reading properties of undefined values, make sure the data you're accessing is actually in the format you're expecting.为避免读取未定义值的属性,请确保您正在访问的数据实际上是您期望的格式。 In this case, it looks like 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.Blocksdata['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.

相关问题 UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined 我有这个错误: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined - I have this error: UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'buffer' of undefined 为什么会收到此错误类型错误:无法读取未定义的属性“客户端” - Why am getting this error TypeError: Cannot read property 'client' of undefined 我为什么会收到TypeError:无法读取未定义的属性“现在” - Why am I getting TypeError: Cannot read property 'now' of undefined 为什么我收到“ TypeError:无法读取未定义的属性” length” - Why am I getting 'TypeError: Cannot read property 'length' of undefined' 为什么我有这个错误:(节点:16040)UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性“标题” - Why i"m having this error: (node:16040) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'title' of undefined javascript UnhandledPromiseRejectionWarning:TypeError:无法读取未定义的属性'forEach' - javascript UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined UnhandledPromiseRejectionWarning:TypeError:无法读取未定义 Discord.JS 的属性“forEach” - UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'forEach' of undefined Discord.JS 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性'john' - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM