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?

2 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?”

Leave a Comment