NodeJS - Iterate Object

Berk

c00l c4n u g1mme ur c0d3 plox
Developer
Messages
734
Likes
124
#1
Hello.
I have an object that has 50+ items in it with values (made in java, from database) and I need to put each item in session in expressJS, and I don't know how as I'm pretty new to NodeJS, how can I achieve this?
P.S: I tried google, I couldn't understand how to do it.
 

Wess

BYPASSING OVERRIDE
Staff member
FindRetros Moderator
Messages
3,688
Likes
1,902
#2
Can't you just do req.session = yourObject?

You really need to provide more information, such as the object itself.
 

LeChris

Habbo Enthusiast
Messages
2,566
Likes
1,103
#3
You can go through an objects properties using a for loop, but I'm willing to bet this issue could be solved by what @Holmes said and just using a middleware to modify req.session
 

Berk

c00l c4n u g1mme ur c0d3 plox
Developer
Messages
734
Likes
124
#4
Code:
object:
   { id: '5b7a80e30a975a4277ea72d8',
     name: 'Berkay Gündüz',
     nickname: 'berkaygndzs',
     email: '[email protected]',
     social: 'GOOGLE',
     phone: null,
     phoneCode: null,
     phoneNumber: null,
     password: null,
     profileImage: 'https://s3-eu-west-1.amazonaws.com/pingwebappbucket/images/profile/5b7a80e30a975a4277ea72d8/gf8q5mq5ccvjqrqktp3er0i9p5.jpg',
     coverImage: 'https://s3-eu-west-1.amazonaws.com/pingwebappbucket/images/cover/5b7a80e30a975a4277ea72d8/e074g9n1pphm147l0ur0u3bdh4.jpg',
     status: 'Frontend Developer @ Ping X',
     birthday: '2002-04-18',
     city: 'Gaziantep',
     country: 'Turkey',
     education: 'Upper secondary education',
     mariageStatus: 'Single',
     isOnline: true,
     rating: 7239,
     rankTitle: 'Rookie',
     weeklyRating: 611,
     token: '$2a$10$OfiwPiPJbQQxSG9kBeqUMuu15m0kUQGAjqoXOd0oneD6mwyPzaQ/y',
     userId: '102483485784565949008',
     lastCallDate: '2018-09-04T09:00:57+0000',
     registrationToken: 'fyVbFd04ZrY:APA91bGjAXARB-xH6GmQN7U3i-B-8C2mLs9aMKBxEHpwbXAfWNqD2R1THC4X8b_naDsHwOir429_MBfFkzIWvDrfIAXOJOuaoshyUhN5D9bAClTeqVef2N6VmjVqLNzzHUDOFkRv0i54',
     countOfFollowers: 1,
     countOfUsersToFollow: 1,
     recoverToken: 'YmJ5LmduZHpAZ21haWwuY29taWw5OWNvM3A4Zmw2ZmQ0OWU5Z3Zrbm5iYmQ=',
     userState: null,
     lowerNickname: 'berkaygndzs',
     nicknameUpdateDate: 1534755051541,
     latitude: 37.0500297,
     longitude: 37.3289751,
     helpLatitude: 37.0500358,
     helpLongitude: 37.3289629,
     trustedLatitude: 37.0500297,
     trustedLongitude: 37.3289751,
     deleteDay: null,
     countOfPings: 0,
     countOfEndorsement: 5,
     countOfHelpResponse: 0,
     countOfSetCoordinates: 309,
     countOfAlerts: 0,
     countOfVerifications: 0,
     isVisibleForSpecified: false,
     invisibleFromDate: 1535881091655,
     invisibleToDate: 4691554691655,
     visibleListsIds: null,
     achievementsArray: [],
     countOfBadWords: 0,
     countOfBadPings: 0,
     appVersion: 53,
     invitationId: null,
     isVisibleToTrusted: false,
     isFollowing: false,
     isUserToFollow: false,
     isBlocked: false,
     isIgnored: false,
     amIBlocked: false,
     isSubscribed: false,
     isTrusted: false,
     oldPassword: null,
     isDeleteProfileImage: null,
     isDeleteCoverImage: null,
     percentRating: 0,
     isWelcomeMessageSent: true,
     registrationTokenValid: true },
  newUser: false }
This is the object, I know some are useless but I need them, and
Code:
 req.session = body.object
didn't work.
 

Wess

BYPASSING OVERRIDE
Staff member
FindRetros Moderator
Messages
3,688
Likes
1,902
#5
Code:
object:
   { id: '5b7a80e30a975a4277ea72d8',
     name: 'Berkay Gündüz',
     nickname: 'berkaygndzs',
     email: '[email protected]',
     social: 'GOOGLE',
     phone: null,
     phoneCode: null,
     phoneNumber: null,
     password: null,
     profileImage: 'https://s3-eu-west-1.amazonaws.com/pingwebappbucket/images/profile/5b7a80e30a975a4277ea72d8/gf8q5mq5ccvjqrqktp3er0i9p5.jpg',
     coverImage: 'https://s3-eu-west-1.amazonaws.com/pingwebappbucket/images/cover/5b7a80e30a975a4277ea72d8/e074g9n1pphm147l0ur0u3bdh4.jpg',
     status: 'Frontend Developer @ Ping X',
     birthday: '2002-04-18',
     city: 'Gaziantep',
     country: 'Turkey',
     education: 'Upper secondary education',
     mariageStatus: 'Single',
     isOnline: true,
     rating: 7239,
     rankTitle: 'Rookie',
     weeklyRating: 611,
     token: '$2a$10$OfiwPiPJbQQxSG9kBeqUMuu15m0kUQGAjqoXOd0oneD6mwyPzaQ/y',
     userId: '102483485784565949008',
     lastCallDate: '2018-09-04T09:00:57+0000',
     registrationToken: 'fyVbFd04ZrY:APA91bGjAXARB-xH6GmQN7U3i-B-8C2mLs9aMKBxEHpwbXAfWNqD2R1THC4X8b_naDsHwOir429_MBfFkzIWvDrfIAXOJOuaoshyUhN5D9bAClTeqVef2N6VmjVqLNzzHUDOFkRv0i54',
     countOfFollowers: 1,
     countOfUsersToFollow: 1,
     recoverToken: 'YmJ5LmduZHpAZ21haWwuY29taWw5OWNvM3A4Zmw2ZmQ0OWU5Z3Zrbm5iYmQ=',
     userState: null,
     lowerNickname: 'berkaygndzs',
     nicknameUpdateDate: 1534755051541,
     latitude: 37.0500297,
     longitude: 37.3289751,
     helpLatitude: 37.0500358,
     helpLongitude: 37.3289629,
     trustedLatitude: 37.0500297,
     trustedLongitude: 37.3289751,
     deleteDay: null,
     countOfPings: 0,
     countOfEndorsement: 5,
     countOfHelpResponse: 0,
     countOfSetCoordinates: 309,
     countOfAlerts: 0,
     countOfVerifications: 0,
     isVisibleForSpecified: false,
     invisibleFromDate: 1535881091655,
     invisibleToDate: 4691554691655,
     visibleListsIds: null,
     achievementsArray: [],
     countOfBadWords: 0,
     countOfBadPings: 0,
     appVersion: 53,
     invitationId: null,
     isVisibleToTrusted: false,
     isFollowing: false,
     isUserToFollow: false,
     isBlocked: false,
     isIgnored: false,
     amIBlocked: false,
     isSubscribed: false,
     isTrusted: false,
     oldPassword: null,
     isDeleteProfileImage: null,
     isDeleteCoverImage: null,
     percentRating: 0,
     isWelcomeMessageSent: true,
     registrationTokenValid: true },
  newUser: false }
This is the object, I know some are useless but I need them, and
Code:
 req.session = body.object
didn't work.
How did it not work? Did it return an error? Did it crash? What is the console saying?

Like I said to you a lot of times before, you need to give us more information. Basically, you're asking us to solve an issue you have, without any information or code to go on. Show us the code where you try to set the session, show us where you try to access the session/output session data, show is the console, show us the output, etc etc.

We want to help you, but if you keep on providing almost no information I'll start closing your threads.
 

Berk

c00l c4n u g1mme ur c0d3 plox
Developer
Messages
734
Likes
124
#6
How did it not work? Did it return an error? Did it crash? What is the console saying?

Like I said to you a lot of times before, you need to give us more information. Basically, you're asking us to solve an issue you have, without any information or code to go on. Show us the code where you try to set the session, show us where you try to access the session/output session data, show is the console, show us the output, etc etc.

We want to help you, but if you keep on providing almost no information I'll start closing your threads.
Nothing happened when I did the request with request module, post request. my code is
upload_2018-9-4_13-2-52.png
There is a empty cookie with empty value, as well.
 

Wess

BYPASSING OVERRIDE
Staff member
FindRetros Moderator
Messages
3,688
Likes
1,902
#7
You're setting a cookie there with the object, so what is it exactly that you want, a cookie or a session? Because you said session, I assumed you were using [ To view this link you must register here. ]
 

Jian

Resident Weeb
Contributor
Messages
668
Likes
419
#8
If you want to store it in req and have it access it through all routes, you have to do it via middleware.
 

Berk

c00l c4n u g1mme ur c0d3 plox
Developer
Messages
734
Likes
124
#9
You're setting a cookie there with the object, so what is it exactly that you want, a cookie or a session? Because you said session, I assumed you were using [ To view this link you must register here. ]
I'm sorry, mentioned wrong thing but yeah, I need to set cookies over this.
I used this and worked perfectly, to anyone wondering:
Code:
for (const key in body.object) {
if (body.object.hasOwnProperty(key)) {
res.cookie(key,body.object[key]);
}
}
Thanks for the help.
 

LeChris

Habbo Enthusiast
Messages
2,566
Likes
1,103
#10
I'm sorry, mentioned wrong thing but yeah, I need to set cookies over this.
I used this and worked perfectly, to anyone wondering:
Code:
for (const key in body.object) {
if (body.object.hasOwnProperty(key)) {
res.cookie(key,body.object[key]);
}
}
Thanks for the help.
Figured the for loop would do it was just unsure on the data manipulation you had aha
 
Messages
1,365
Likes
642
#11
You can just use object destructuring?

PHP:
const body = {};
req.session = { ...req.session, ...body };
But why would you apply the entire user object to req.session itself? apply it to another key req.session.user
 
Last edited by a moderator:
Messages
415
Likes
86
#12
I'm sorry, mentioned wrong thing but yeah, I need to set cookies over this.
I used this and worked perfectly, to anyone wondering:
Code:
for (const key in body.object) {
if (body.object.hasOwnProperty(key)) {
res.cookie(key,body.object[key]);
}
}
Thanks for the help.
I'm not entirely sure on expressJS, but I'm pretty sure setting a cookie for every parameter in that object is extremely bad security practice as you are literally setting them all as plain text cookies stored in the users browser. Use the session class. Please.
 
You can just use object destructuring?

PHP:
const body = {};
req.session = { ...req.session, ...body };
But why would you apply the entire user object to req.session itself? apply it to another key req.session.user
Alternatively instead of destructuring the body object and appending it directly to the array could you not set req.session.user to your user object?
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top