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 });
      }
    });
    
};
1 Like

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".

I also got the same error as you. How did you fix it?

1 Like

I got the exact same error,
my models were being called correctly everything was correct except

userModel.js

module.exports = mongoose.model( "user" , userSchema );

orderModel.js

customer: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "User"
}

spelling error in the reference. 1 hour wasted on a capital letter left out in the past :laughing: :rofl: :joy: :sweat_smile: :smiling_face_with_tear: :cry: :sob:

1 Like

Hi, my name is Mohsin Hassan Khan, If you’re encountering the “MissingSchemaError” while trying to reference a collection from another MongoDB database, follow these steps:

  1. Ensure you’ve established separate database connections for both databases using mongoose.createConnection.
  2. Import these connections into your current file if they’re defined in another file.
  3. When defining your Mongoose schema, correctly reference the collection from the other database using yourConnection.model(modelName) within the ref field.

Here’s an example:

const { productsDbConnection, usersDbConnection } = require("../db") ; // connections imported from db file //

const UserSchema = new mongoose.Schema({
  // ...
  cart: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: productsDbConnection.model('women_collection') // Make sure you have registered the women_collection model in you Db//
    },
  ],
});

Thanks, Please let me know if this helps anyone.