0

I am trying to save a leader board made of objects nested in an array. I want to save it in my database, but I have not been able to create the right schema and I don't think I that is the best way to go. When I run the code I get the error of:

"LeaderBoardSchema is not a constructor".

What is the appropriate way of creating a schema that I need.

I have tried many variations looking online, but I keep getting the " LeaderBoardSchema is not a constructor".

The examples from other questions on S.O have not been able to help me much.

 
 
 // leaderboard object that i want to save
 leaderBoard = [
        leaderOne=   {
            name: 'Ultimate Beast',
            score: 2
        },
        leaderTwo=  {
            name: 'Masked Titan',
            score: 9
        },
        leaderThree=  {
            name: 'Oranolio',
            score: 7
        },
        leaderFour=  {
            name: 'Popularkiya',
            score:1
        },
        leaderFive=  {
            name: 'Bootecia',
            score: 11
        },
    ];




// Database schema 


const Schema = mongoose.Schema()

const LeaderBoardSchema = new mongoose.Schema({
   leaderBoard:{
    leaderOne :   {
        name: String,
        score: Number
    },
    leaderTwo :  {
        name: String,
        score: Number
    },
    leaderThree :  {
        name: String,
        score: Number
    },
    leaderFour :  {
        name: String,
        score:Number
    },
    leaderFive :  {
        name: String,
        score: Number
    }
   }
  }, {collection: 'leaderboard-data'});

const PlayerData = mongoose.model('LeaderBoard Data', LeaderBoardSchema);



// My attempt 


const leaderBoardToSave = new LeaderBoardSchema({
        leaderBoard:{
            leaderOne :   {
                name: 'asdf',
                score: 12
            },
            leaderTwo :  {
                name: 'sfgh',
                score: 12
            },
            leaderThree :  {
                name: 'weh',
                score: 12
            },
            leaderFour :  {
                name: 'asdf',
                score:12
            },
            leaderFive :  {
                name: 'asdf',
                score: 12
            }
        }
    })

1 Answer 1

1

Currently your leaderBoard field is an object. To model an array of objects do the following with your schema:

const LeaderBoardSchema = new mongoose.Schema({
   leaderBoard: [
    {
        name: String,
        score: Number
    }
   ]
  }, {collection: 'leaderboard-data'});

as for the issue with the schema constructor. You're creating the mongoose model as follows const PlayerData = mongoose.model('LeaderBoard Data', LeaderBoardSchema);. But then you do new LeaderBoardSchema({...}). You need to use the mongoose model PlayerData instead. so to create a new leaderboard:

const leaderBoardToSave = new PlayerData({
        leaderBoard: [
           {
              name: 'asdf',
              score: 12
           },
           {
              name: 'gds',
              score: 12
           },
           {
              name: 'adad',
              score: 12
           },
       ]
    })
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for clearing that up for me. Got it to work!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.