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?

3 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. You’re still in the process of declaring the variables in your object, to prevent this; create a blank object and populate it and/or set undefined/null and add to them later.

    //let fun = {
    //  "user":2,
    //  "wontwork":fun.user // this will equal undefined and throw errors but looks clever right?
    //}
    // Let's try the following:
    
    let fun ={};
    fun.test = "hellow world";
    console.log(fun.test);
    
    fun.tester = function() {
    console.log("wow");
    };
    
    fun.tester();
    let anotherway = {
      "test":undefined,
      "name":undefined,
      "age": 20
    };
    
    console.log(anotherway.test);
    anotherway.test = "1";
    anotherway.test = "2";
    anotherway.test = "3";
    // Now equals 3
    console.log(anotherway.test);
    
    anotherway.test = () => console.warn("don't be alarmed with this warning");
    console.log(anotherway.test);
    anotherway.test();
    let fun = {"user":2,"wontwork":fun.user};
    Reply

Leave a Comment