[英]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.这里有两件不同的事情。
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)
.
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.