1

am new here. what i am trying to do is to reference a sub-document and then populate on the .find() function.

My branch schema

const branchSchema = mongoose.Schema(
  {
    office: [
      {
        ...multiple office detail
      },
    ],
  },
  { timestamps: true }
)

const Branch = mongoose.model("Branch", branchSchema)
export default Branch

My company schema

const companySchema = mongoose.Schema(
  {
    office: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Branch.office",
    },
    status: {
      type: String,
      required: true,
      default: "active",
    },
    createdBy: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "User",
    },
  },
  { timestamps: true },
)

const Company = mongoose.model("Company", companySchema)
export default Company

Query and error:

const offices = await Company.find().populate("office")
ERROR I GET: 
{
    "error": "failed",
    "message": "MissingSchemaError: Schema hasn't been registered for model \"Branch.office\".\nUse 
     mongoose.model(name, schema)"
}

This is pretty much the same question How to Populate on Sub Document's Reference (Mongoose) is it that maybe the latest mongoDB drivers don't support this operation anymore?

0 Answers0