【发布时间】:2019-12-18 18:15:16
【问题描述】:
我有以下控制器方法:
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
);
}));
对于Promise 行,我收到一个错误:
UnhandledPromiseRejectionWarning:TypeError:无法读取属性 'Symbol(Symbol.iterator)' of undefined
知道我的代码有什么问题吗?
【问题讨论】:
-
Array.prototype.forEach不返回任何内容。 -
@ASDFGerte 只是为了弄清楚为什么这是相关的 - 如果它没有返回任何内容,它实际上会返回
undefined。你把undefined给Promise.all。 -
这是否意味着我不应该在这里使用 Promise 来遍历用户?
-
这意味着您应该将所有承诺放入一个数组中并将 that 提供给
Promise.all。你现在所拥有的既不是这里也不是那里 - 你循环一些数据,提出一些请求,然后尝试await,但也使用Promise.all等待一切,但从不给它任何东西等待。看看here
标签: javascript node.js