From 776704f05097b3da5d1673b1480a42d73229ea73 Mon Sep 17 00:00:00 2001 From: Ruslan <40831934+flytestb@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:13:20 +0200 Subject: [PATCH] Fix session error Fix "Cannot destructure property 'session_id' of 'session' as it is null" error Sometimes with a large number of requests, an error occurs when searching for a session. 1. Session not found. "session === null" 2. Next, we try to create it, but we get into "catch", i.e. our "session" variable is "null". If we get the error "Unique constraint failed on the fields: (`session_uuid`)", then the session was created in another request and we need to get it. --- lib/session.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/lib/session.js b/lib/session.js index 7c4476d6..98731618 100644 --- a/lib/session.js +++ b/lib/session.js @@ -50,15 +50,18 @@ export async function getSession(req) { device, }); - if (!session) { - return null; - } } catch (e) { if (!e.message.includes('Unique constraint')) { throw e; + } else { + session = await getSessionByUuid(session_uuid); } } } + + if (!session) { + return null; + } const { session_id } = session;