1. Tired of not being able to see links? Want to chat with other members? Need help?

NodeJS - Iterate Object

Discussion in 'Help & Support' started by Berk, Sep 3, 2018.

  1. Berk

    Berk c00l c4n u g1mme ur c0d3 plox Developer

    Joined:
    Oct 17, 2015
    Messages:
    722
    Likes Received:
    117
    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.

     
  2. Holmes

    Holmes BYPASSING OVERRIDE Staff Member FindRetros Moderator

    Joined:
    Nov 25, 2011
    Messages:
    3,519
    Likes Received:
    1,803
    Can't you just do req.session = yourObject?

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

    LeChris Well-Known Member

    Top Poster Of Month

    Joined:
    Sep 30, 2013
    Messages:
    2,456
    Likes Received:
    1,188
    Best Answer
    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
     
  4. Berk

    Berk c00l c4n u g1mme ur c0d3 plox Developer

    Joined:
    Oct 17, 2015
    Messages:
    722
    Likes Received:
    117
    Code:
    object:
       { id: '5b7a80e30a975a4277ea72d8',
         name: 'Berkay Gündüz',
         nickname: 'berkaygndzs',
         email: '[email protected]om',
         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.
     
  5. Holmes

    Holmes BYPASSING OVERRIDE Staff Member FindRetros Moderator

    Joined:
    Nov 25, 2011
    Messages:
    3,519
    Likes Received:
    1,803
    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.
     
  6. Berk

    Berk c00l c4n u g1mme ur c0d3 plox Developer

    Joined:
    Oct 17, 2015
    Messages:
    722
    Likes Received:
    117
    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.
     
  7. Holmes

    Holmes BYPASSING OVERRIDE Staff Member FindRetros Moderator

    Joined:
    Nov 25, 2011
    Messages:
    3,519
    Likes Received:
    1,803
    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 [ Want to see this link? Register ! ]
     
  8. Jian

    Jian Resident Otaku Contributor

    Joined:
    Sep 2, 2011
    Messages:
    664
    Likes Received:
    430
    If you want to store it in req and have it access it through all routes, you have to do it via middleware.
     
  9. Berk

    Berk c00l c4n u g1mme ur c0d3 plox Developer

    Joined:
    Oct 17, 2015
    Messages:
    722
    Likes Received:
    117
    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.
     
  10. LeChris

    LeChris Well-Known Member

    Top Poster Of Month

    Joined:
    Sep 30, 2013
    Messages:
    2,456
    Likes Received:
    1,188
    Figured the for loop would do it was just unsure on the data manipulation you had aha
     
    Holmes likes this.
  11. ToastDick

    ToastDick Hell on Earth

    Joined:
    Oct 18, 2016
    Messages:
    1,268
    Likes Received:
    665
    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: Sep 4, 2018
  12. Muffinss

    Muffinss yes

    Joined:
    Mar 16, 2012
    Messages:
    371
    Likes Received:
    90
    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.
    --- Double Post Merged, Sep 6, 2018, Original Post Date: Sep 6, 2018 ---
    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?
     
    ToastDick likes this.

Share This Page

Loading...