From 12bececdeae084f49b1eff4780da43e93c4660e1 Mon Sep 17 00:00:00 2001 From: Linnea Myllynen <55977969+linneamyl@users.noreply.github.com> Date: Mon, 5 Sep 2022 10:56:35 +0300 Subject: [PATCH] Fix cannot set headers after sent to client error (#48) * Fix cannot set headers after sent to client error * Refactor iterating through userguids * Refactor marking tasks done --- src/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/index.js b/src/index.js index 7d329b9..dfdfbd5 100644 --- a/src/index.js +++ b/src/index.js @@ -282,8 +282,8 @@ const main = async () => { const memberGroup = req.body // iterate through membergroups and user ids // Mark the task as completed for all the users - for (let userIds of Object.values(memberGroup)) { - const promises = userIds.map((user_guid) => + const promises = Object.values(memberGroup).map((userIds) => + userIds.map((user_guid) => Promise.resolve( postTaskEntry({ user_guid, @@ -293,9 +293,10 @@ const main = async () => { }) ) ) - const entries = await Promise.all(promises) - res.json(entries).status(200) - } + ) + const flattedPromises = promises.flat() + const entries = await Promise.all(flattedPromises) + res.json(entries).status(200) } catch (e) { res.status(e.statusCode).send(e.message) }