I have 2 variables in the same file that reference each other, but I keep getting an error that the latter one is undefined. How do I solve this?

I have a seed file in my project where I store dummy data to use in a mock mongoDB database. I have a "comedians" array full of comedian information and a "users" array full of user information. My relevant code looks like this:

let comedians = [
{
        _id: mongoose.Types.ObjectId(),
        name: "Jerry Seinfeld",
        description: "Jerry Seinfeld is best known for playing a semi-fictionalized version of himself in the sitcom Seinfeld. As a standup, Seinfeld specializes in observational comedy and is revered as one of the best known and critically acclaimed comedians of his generation.",
        specials: [
            {
                specialTitle: "I'm Telling You for the Last Time",
                specialDescription: "Months after his classic TV sitcom ends, the legendary comic returns to his stand-up roots to deliver his best jokes on a sold-out international tour.",
                specialRatings: [
                    {
                        userId: users[0]._id,
                        rating: 2
                    },
                    {
                        userId: users[1]._id,
                        rating: 5
                    }
                ],
              
            },            
        ],
        comments: [
            {
                commentAuthor: "Julia Smith",
                commentDate: "October 7th, 2020 at 2:30pm",
                commentContent: "I Love This!",
                commentLikes: [
                    users[0]._id,
                    users[1]._id,
                    users[2]._id
                ]
            }
        ],
        metrics: {
            favoritesReceived: [
                users[0]._id,
                users[1]._id,
                users[2]._id,
                users[3]._id,
                users[4]._id
            ],
            views: 300
        }
    }
];

let users = [
{
        _id: mongoose.Types.ObjectId(),
        username: "userOne",
        password: "userOnePassword",
        favorited: [
            comedians[0]._id,
            comedians[1]._id,
            comedians[5]._id
        ],
        recentlyViewed: [
            comedians[6]._id,
            comedians[7]._id,
            comedians[9]._id,
            comedians[10]._id
        ],
    }
]

The problem is that every time I try to run the test, I get an error saying that "users" is undefined. I understand that this is because I try to access it in the comedians array before it is initialized, but I can’t simply move the users array to come before the comedians array, because then I’ll be trying to access the comedians array before it’s initialized. I’m not sure how to get around this, any suggestions?

157 thoughts on “I have 2 variables in the same file that reference each other, but I keep getting an error that the latter one is undefined. How do I solve this?”

  1. Pingback: keto chipotle bowl
  2. Thanks for finally talking about > I have 2 variables in the same file that reference each other, but I keep getting an error that the latter one
    is undefined. How do I solve this? – JavaScript
    Help < Loved it!

    Reply
  3. Excellent beat ! I wish to apprentice even as you amend your website,
    how could i subscribe for a weblog web site? The account helped me
    a acceptable deal. I have been a little bit acquainted of this your broadcast offered
    vibrant transparent concept

    Reply
  4. Great blog! Do you have any suggestions for aspiring writers?
    I’m planning to start my own blog soon but I’m a little
    lost on everything. Would you advise starting with a free platform like
    Wordpress or go for a paid option? There are so many choices out there that I’m totally confused ..
    Any recommendations? Bless you!

    Reply
  5. Can I simply say what a relief to uncover someone that actually understands what they
    are talking about on the net. You certainly
    realize how to bring an issue to light and make it important.
    More and more people have to read this and understand
    this side of the story. It’s surprising you aren’t more
    popular given that you certainly possess the gift.

    Reply

Leave a Comment