Currently, I am working on a project and need to extract a list of user ids of all currently logged in users. I came across a relevant question on StackOverflow at this link. However, I am facing difficulties in accessing the object properties as intended.
My current code snippet looks like this:
let sessions = req.sessionStore.sessions
console.log(JSON.stringify(sessions))
Upon running this code, the console outputs a JSON object containing information about the logged-in users.
From the output, it's clear that there are two logged-in users. My objective now is to loop through the object and print out the user ids located within the passport
property to the console.
I attempted the following code:
for (sesh in sessions) {
console.log(sessions[sesh].passport.user.id)
}
However, this code snippet returns undefined. I also tried using sessions.forEach()
, but unfortunately, that didn't work either.
Can anyone point out what I am doing wrong? Do I need to parse the JSON object before accessing the user ids?