skip to Main Content

I need a little help, I’ve a category schema, which looks like below,

let mongoose = require("mongoose");
let schema = mongoose.Schema;
const _ = require("lodash");

let category = new schema(
  {
    // Primary Fields
    categoryName: { type: String, unique: true },
    isArchived: { type: Boolean, default: false },
    isDeleted: { type: Boolean, default: false },
    categoryImage: { type: String },
    categorySlug: { type: String, unique: true },
    createdBy:{
        type: mongoose.Types.ObjectId,
        ref: "Users",
    }
  },
  {
    timestamps: true,
  }
);

let Category = mongoose.model("Categories", category);
module.exports = {
    Category,
};

and I’m using this reference into another schema, which looks like below,

let mongoose = require("mongoose");
let schema = mongoose.Schema;
const { ObjectID } = require("mongodb");

let coupons = new schema({
    city: {type:String},
    categoryId: {
        type: mongoose.Types.ObjectId,
        ref: "categories",
    },
    offerCode: {type:String,unique:true},
    couponName: {type:String,unique:true},
    validFrom: {type:Date},
    validTill: {type:Date},
    visibility: { type:Boolean,default:false},
    issuedFor: {type:String,enum:['BIKE','AUTO','DELIVERY','CHARGING']},
    applicability: {type:String,enum:['1','2','3','4','5','6','7','8','9','10','0']},
    bankType: {type:String},
    isArchived: { type: Boolean, default: false },
    isDeleted: { type: Boolean, default: false },
    createdBy: {
        type: mongoose.Types.ObjectId,
        ref: "Users",
    },
},
{
    timestamps: true,
});
    coupons.index( { "offerCode": 1,"couponName": 1,"validFrom": 1, "validTill":1, "categoryId":1 }, { unique: true } );
    let Coupons = mongoose.model("Coupons", coupons);
    module.exports = {
         Coupons
    };

everything looks pretty much good, I can use reference of other schema but the category schema is throwing MongooseError whenever I’m trying to populate this from my code.

The code looks like below,

return await Coupons.find(query)
        .populate('categoryId')
        .skip(parseInt(skip)).select(select).limit(parseInt(limit)).sort({ createdAt: -1 })

can anyone please tell me what is wrong that leads me towards MongooseError [MissingSchemaError]: Schema hasn't been registered for model "categories".

Please help me. Thanks in advance.

2

Answers


  1. The correct format would be:

    categoryId: {
            type: mongoose.Schema.Types.ObjectId,
            ref: "Category",
        },
    

    You need to change the name of your model too:

    let Category = mongoose.model("Category", category);
    

    Mongoose will look for the pularized, lowercase collection of the model name, so mongoose assumes Category -> categories.

    The first argument is the singular name of the collection your model is for. Mongoose automatically looks for the plural, lowercased version of your model name. Thus, for the example above, the model Tank is for the tanks collection in the database.

    Then when you do the populate you can always explicitly specify the path and model like so

    return await Coupons.find(query)
            .populate({path: 'categoryId', model: Category})
            .skip(parseInt(skip)).select(select).limit(parseInt(limit)).sort({ createdAt: -1 })
    

    See Compiling your first model and Saving refs for full explanation.

    Login or Signup to reply.
  2. from this code mongoose.model("Categories", category);, you are defining this model with the name of Categories. so you will have to use the same name wherever you want to reference it.
    this below code should work for you.

    categoryId: {
            type: mongoose.Types.ObjectId,
            ref: "Categories", // note: C is capitalized. 
        },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search