Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Populate in Mongoose doesn't work if I'm using Typescript Error: Schema hasn't been registered for model "Category" #15260

Open
2 tasks done
Eligio93 opened this issue Feb 14, 2025 · 1 comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary

Comments

@Eligio93
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.9.1

Node.js version

22.14.0

MongoDB server version

6.12.0

Typescript version (if applicable)

5

Description

I'm using NextJs 15 with TypeScript and Mongoose, but when I try to populate the Product fields with the category, I get the following error: Error: Schema hasn't been registered for model "Category". Use mongoose.model(name, schema). Also I've realized that if I comment out .populate() and then add it back when the server is already running, it works perfectly. So seems the problem is at the moment of compiling code.

Steps to Reproduce

This is the Product Schema:

import mongoose from "mongoose";
import { Schema } from "mongoose";
import ProductInterface from "@/interfaces/product.interface";

const ProductSchema = new Schema<ProductInterface>({
  name: { type: String, required: true },
  description: { type: String, required: true },
  brand: { type: String, required: true },
  price: { type: Number, required: true },
  images: [{ type: String, required: true }],
  category: { type: Schema.Types.ObjectId, ref: "Category", required: true },
  gender: { type: String, required: true },
  stock: { type: Number, required: true },
  rating: { type: Number, required: true, max: 5 },
  discount: { type: Number },
  isFeatured: { type: Boolean, required: true },
  views: { type: Number, required: true },
  isLive: { type: Boolean, required: true },
});

const Product =
  mongoose.models.Product ||
  mongoose.model<ProductInterface>("Product", ProductSchema);

export default Product;

This is my category Schema:

import mongoose from "mongoose";
import { Schema } from "mongoose";
import CategoryInterface from "@/interfaces/category.interface";

const CategorySchema = new Schema<CategoryInterface>({
  name: { type: String, required: true },
  description: { type: String, required: true },
  image:{type:String,required:true},
  isFeatured: { type: Boolean, required: true },
});

const Category =
  mongoose.models.Category ||
  mongoose.model<CategoryInterface>("Category", CategorySchema);

export default Category;

and this is my use case:


  await connectDB();
  const newArrivals/*: HydratedDocument<ProductInterface & { category: CategoryInterface }>[] */ = await Product.find({ isLive: true })
    .sort({ _id: -1 })
    .limit(6)
    .populate('category')
    .exec();

Expected Behavior

No response

@vkarpov15
Copy link
Collaborator

How are you importing these models? The most likely explanation is that you haven't imported the Category model when you're running the await Product.find().populate('category') query

@vkarpov15 vkarpov15 added the help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary label Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help This issue can likely be resolved in GitHub issues. No bug fixes, features, or docs necessary
Projects
None yet
Development

No branches or pull requests

2 participants