Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:43:41+00:00 2026-06-09T11:43:41+00:00

this is my first post so thanks in advance for the help :) I’m

  • 0

this is my first post so thanks in advance for the help 🙂

I’m trying to make my main.js file and to make things easier I want to do something like this:

var pages = {
"/profile":{"page":"My Profile","loc":"./contentPages/profile"},
...
...
...
};

for (var item in pages) {
    app.get(item, function(req, res){
        if(req.session.user_id == null){
            res.redirect('/');
            return;
        }


        res.render(pages[item].loc, {
        title:pages[item].page,
        thisPage:pages[item].page
        });
    });
}

right now no matter what happens whatever I have last in the dictionary is the the page which is always rendered. Is there a way to do something like this or must I write out every page I want to create?

Thanks so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:43:42+00:00Added an answer on June 9, 2026 at 11:43 am

    The problem is that all the closures created by function(req, res) refer to the same variable (item) so they all use whatever value it has when they’re called rather than what it had when they were created, and that’s going to be the last one.

    Most straightforward workaround is to create the closures with a helper function:

    function makeHandler(item) {
      return function(req, res) {
         // copy body of handler function here
      };
    }
    

    and then replace the body of your loop with:

    app.get(item, makeHandler(item));
    

    Now each handler gets its own private copy of item which always retains the value it had when it was created.

    You could also use an immediate function invocation to create the closures, but that would make the code look a little more cluttered.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First, a thank you in advance. Second, this is my first post so apologies
This is my first post here so go easy. I am trying to build
Thanks in advance for any help... I'm trying to (1) generate a begin time
This is my first post in this forum, so please, be patient with me.
This is my first post and I my first experience with jquery. I have
This is my first post on stackoverflow, so please excuse me if my question
This is my first post, thx, you've been very helpful. But I'm stuck. I
This is my first post on Stack Overflow and I'm just wondering on the
This is my first post here. I have a problem. I need to take
this is my first post here on stackoverflow and am very impressed by the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.