Search code examples
mongoosemongoose-schema

Issue while fetching schema instance from mongoose


I've been trying to fetch a mongoose schema instance

var UserScore = new Schema({ 
    userName: String,
    score: Number
});

module.exports = mongoose.model('UserScore', UserScore);


The DB does have records 
{
    "userId": "doe@doe.com",
    "score": 30
},
{
    "userId": "doe@doe.com",
    "score": 40
}

However, when I try to fetch using the below code it does not load

    UserScore.find({userId: userId})
        .exec(function (err, userScores) {
            if (err) { return next(err); }
            console.log(userScores)
    })

Solution

  • This change should fix it. Mongoose automatically looks for the plural, lowercased version of your model name. module.exports = mongoose.model('UserScore', UserScore, 'UserScore'); You can force the name if you want. Or just follow the plural form as prescribed by mongoose.