简体   繁体   English

为什么 Promise.all 的错误?:TypeError:无法读取未定义的属性“Symbol(Symbol.iterator)”

[英]Why the error for Promise.all?: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined

I have the following controller method:我有以下控制器方法:

const org = await organisation.toJSON().name;
// The next line works fine, that's not the problem
const users = await organisation.getUsers(organisation.toJSON().id, User);
await Promise.all(users.forEach(async user => {
  await sendAccountEmail(
    user.email,
    user.surname,
    org
  );
}));

For the Promise line I get an error:对于Promise行,我收到一个错误:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“Symbol(Symbol.iterator)”

Any idea what is wrong with my code?知道我的代码有什么问题吗?

You're using Promise.all incorrectly.您错误地使用了Promise.all That method expects you to give it an array (or another iterable object) filled with Promises, and then it waits until all Promises in that array have settled (resolved or rejected).该方法希望你给它一个填充了 Promise 的数组(或另一个可迭代对象),然后它会一直等待,直到该数组中的所有 Promise 都已解决(已解决或被拒绝)。 You shouldn't be iterating over the array inside the parameter, especially not with forEach which returns undefined, thus making your code equivalent to await Promise.all(undefined) -- so you can see now why that would error, can't you?您不应该迭代参数内的数组,尤其是不使用返回 undefined 的forEach ,从而使您的代码等效于await Promise.all(undefined) -- 所以您现在可以看到为什么会出错,不是吗?

What you should be doing is using Array.map to convert your array of users into an array of Promises that you want to wait for:您应该做的是使用Array.map将您的用户数组转换为您想要等待的Array.map数组:

const userPromises = users.map(user => sendAccountEmail(user.email, user.surname, org));
await Promise.all(userPromises);

Though you already have your answer, I thought I'd explain the actual error you're getting because it's useful to understand what this specific error means and it goes to how Promise.all() actually works.尽管您已经有了答案,但我想我会解释您遇到的实际错误,因为了解此特定错误的含义以及Promise.all()实际工作方式Promise.all()

The error you're getting is this:你得到的错误是这样的:

UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined UnhandledPromiseRejectionWarning: TypeError: 无法读取未定义的属性“Symbol(Symbol.iterator)”

There are two separate things going on here.这里有两件不同的事情。

  1. Promise.all() expects an iterable as its argument, something that it can get an iterator from and iterate the items in it. Promise.all()期望一个 iterable 作为它的参数,它可以从中获取一个迭代器并迭代其中的项目。 Your code is passing the result of .forEach() which is undefined , so the first part of this error is that undefined is not an iterable and the way this is discovered is that it doesn't have a property Symbol(Symbol.iterator) .您的代码正在传递.forEach()的结果undefined ,因此此错误的第一部分是undefined不是可迭代的,并且发现它的方式是它没有属性Symbol(Symbol.iterator) .

  2. The second part of this error is the UnhandlePromiseRejectionWarning .此错误的第二部分是UnhandlePromiseRejectionWarning This shows that await Promise.all(...) rejects and you have no error handling for that.这表明await Promise.all(...)拒绝并且您没有错误处理。 You should either have a try/catch around the await or be using .catch() as in Promise.all().catch() .您应该在await周围进行try/catch或使用.catch()就像在Promise.all().catch() The answer you have accepted does not address this coding problem.您接受的答案并未解决此编码问题。 While fixing your code stops the rejection from happening in normal execution of your code, sendAccountEmail() sounds to me like a function that could run into a failure and could reject.虽然修复您的代码会阻止在您的代码的正常执行中发生拒绝,但sendAccountEmail()对我来说听起来像是一个可能遇到失败并可能拒绝的函数。 Your code needs to handle that case.您的代码需要处理这种情况。 We can't really suggest the best overall error handling strategy here because we'd need to see the larger context of the code to know what is supposed to happen when errors occur.我们不能真正在这里建议最好的整体错误处理策略,因为我们需要查看代码的更大上下文才能知道发生错误时应该发生什么。 But, you need to handle all possible errors.但是,您需要处理所有可能的错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击 React NavLink 会导致错误:Uncaught (in promise) TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) - Clicking on React NavLink causes error: Uncaught (in promise) TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - × TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) - TypeError: Object is not iterable (cannot read property Symbol(Symbol.iterator)) 无法读取未定义的属性 'Symbol(Symbol.iterator)' - Cannot read property 'Symbol(Symbol.iterator)' of undefined 获得未定义是不可迭代的(无法读取属性 Symbol(Symbol.iterator)) - getting undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 节点红色无法读取未定义的属性'Symbol(Symbol.iterator)' - Node red Cannot read property 'Symbol(Symbol.iterator)' of undefined EOSJS和Scatter-无法读取未定义的属性'Symbol(Symbol.iterator)' - EOSJS & Scatter - Cannot read property 'Symbol(Symbol.iterator)' of undefined Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) 不知道该怎么做 - Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator)) Not sure what to do 我收到错误“未捕获的 TypeError:未定义不可迭代(无法读取属性 Symbol(Symbol.iterator))”。 无法找到解决方案 - I am getting an error "Uncaught TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))". Unable to find solution Reactjs函数不能正确返回多个值:TypeError:undefined不可迭代(无法读取属性Symbol(Symbol.iterator)) - Reactjs function not returning multiple values properly: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM