Mongoose how to refer to the same schema

Viewed 267

So my problem statement is something like this: -I have this schema of an object

const categorySchema = new mongoose.Schema({
 name: {
     type: String,
     required: true,
     unique: true,
     minlength: 1,
     maxlength: 100
 },
 parent: {
     type: mongoose.Schema.Types.ObjectId,
     ref: 'categorySchema'
 }
});
  • that parent field should refer to the same schema, but when i want to populate i get this error ( here I know that field parent is on that object )
error: MongooseError [MissingSchemaError]: Schema hasn't been registered for model "categorySchema".
  • and another problem would be, how I can check if that field 'parent' is on the object when I make a query to get all object from that table then populate it. I know manually how to do it, but is there any simpler way?

Thank you!

0 Answers
Related