NodeJS - Iterate Object

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
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.
 

LeChris

github.com/habbo-hotel
Sep 30, 2013
2,725
1,306
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

berkibap#4233
Developer
Oct 17, 2015
863
190
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.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,128
2,456
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

berkibap#4233
Developer
Oct 17, 2015
863
190
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
You must be registered for see images attach

There is a empty cookie with empty value, as well.
 

Weasel

👄 I'd intercept me
Nov 25, 2011
4,128
2,456
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
 

Berk

berkibap#4233
Developer
Oct 17, 2015
863
190
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
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

github.com/habbo-hotel
Sep 30, 2013
2,725
1,306
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
 

MayoMayn

BestDev
Oct 18, 2016
1,423
683
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:

M8than

yes
Mar 16, 2012
463
102
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

Top