MissingSchemaError: Schema hasn't been registered for model UserAddress.address

Been pulling my hair out for hours now, just can’t figure out why the field refuses to populate. What I want to do is return the AddressId field populated with values instead of just an ID, but nothing I’ve tried works, none of the solutions I found do anything.

If you need any other code from the project, I will update the question. Any help is highly appreciated.

Order Model:

const mongoose = require("mongoose");

const orderSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User",
      required: true,
    },
    addressId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "UserAddress.address",
      required: true,
    },
    totalAmount: {
      type: Number,
      required: true,
    },
    items: [
      {
        productId: {
          type: mongoose.Schema.Types.ObjectId,
          ref: "Product",
        },
        payablePrice: {
          type: Number,
          required: true,
        },
        purchasedQty: {
          type: Number,
          required: true,
        },
      },
    ],
    paymentStatus: {
      type: String,
      enum: ["Pending", "Completed", "Cancelled", "Refund"],
      required: true,
    },
    paymentType: {
      type: String,
      enum: ["CoD", "Card", "Wire"],
      required: true,
    },
    orderStatus: [
      {
        type: {
          type: String,
          enum: ["Ordered", "Packed", "Shipped", "Delivered"],
          default: "Ordered",
        },
        date: {
          type: Date,
        },
        isCompleted: {
          type: Boolean,
          default: false,
        },
      },
    ],
  },
  { timestamps: true }
);

module.exports = mongoose.model("Order", orderSchema);

Address Model:

const mongoose = require("mongoose");

const addressSchema = new mongoose.Schema({
  name: {
    type: String,
    required: true,
    trim: true,
    min: 10,
    max: 60,
  },
  mobileNumber: {
    type: String,
    required: true,
    trim: true,
  },
  pinCode: {
    type: String,
    required: true,
    trim: true,
  },
  locality: {
    type: String,
    required: true,
    trim: true,
    min: 10,
    max: 100,
  },
  address: {
    type: String,
    required: true,
    trim: true,
    min: 10,
    max: 100,
  },
  cityDistrictTown: {
    type: String,
    required: true,
    trim: true,
  },
  state: {
    type: String,
    required: true,
    required: true,
  },
  landmark: {
    type: String,
    min: 10,
    max: 100,
  },
  alternatePhone: {
    type: String,
  },
  addressType: {
    type: String,
    required: true,
    enum: ["home", "work"],
    required: true,
  },
});

const userAddressSchema = new mongoose.Schema(
  {
    user: {
      type: mongoose.Schema.Types.ObjectId,
      required: true,
      ref: "User",
    },
    address: [addressSchema],
  },
  { timestamps: true }
);

mongoose.model("Address", addressSchema);
module.exports = mongoose.model("UserAddress", userAddressSchema);

Code that runs the query:

const Order = require("../models/order");
const Cart = require("../models/cart");
const Address = require("../models/address");
const Product = require("../models/product");

exports.getOrders = (req, res) => {
  Order.find({ user: req.user._id })
    .select("_id paymentStatus paymentType orderStatus items addressId")
    .populate("items.productId", "_id name productImages")
    .populate("addressId")
    .exec((error, orders) => {
      if (error) {console.log(error) 
        return res.status(400).json({ error });}
      if (orders) {
        res.status(200).json({ orders });
      }
    });
    
};

First, I know nothing about mongoose so what I suggest might be completely wrong.

You register addressSchema as the Address model. Everywhere ref: is used, it looks like a model name, rather than the field name of another model.

So I would try first to use ref:"UserAddress.Address" rather than ref:"UserAddress.address", that is the name you use in

rather than the name you use in

If that fails, I would try ref:"Address" because you do

You might need to export it like you do for "UserAddress".