Mongoose引用外部项目模型报错求助:NestJS提示未注册Customer模型
When attempting to populate the userID or linkedAccounts fields in your Referral model (which reference the "Customer" model from the main backend service), you encounter this error:
Schema hasn't been registered for model "Customer". Use mongoose.model(name, schema)
Even after updating the ref value to match the collection name "customers", the error persists.
Mongoose requires the schema definition of the referenced model to be registered in the current application context (your referral microservice) to handle population. Since your referral service is a separate instance from the main backend, it doesn't have access to the Customer model's schema by default—even though the collection exists in the database. Changing the ref to the collection name won't fix this because Mongoose still needs the registered model schema to map the data.
Solution 1: Define a Minimal Customer Schema in the Referral Microservice
You don't need the full Customer schema, just a minimal version registered under the correct model name. Add this file to your referral service:
// customer.model.js (referral microservice) import mongoose from "mongoose"; // Define a minimal schema (include only fields you need to populate, or leave empty) const CustomerSchema = new mongoose.Schema({ name: String, email: String }, { collection: "customers" }); // Explicitly match the collection name from the main service // Register the model with the exact name used in your Referral schema's `ref` field mongoose.model("Customer", CustomerSchema);
Import this model before using population in your referral service logic:
import './customer.model.js'; // Import first to register the model import RefModel from './referral.model.js'; // Now population will work const referral = await RefModel.findById(referralId).populate('userID');
Solution 2: Use a Shared Schema Library
For long-term maintainability (especially if multiple services need access to common schemas), extract the Customer schema into a shared module that both the main and referral services can import.
Example directory structure:
shared/ schemas/ customer.schema.js backend-main/ // Imports shared customer schema backend-referral/ // Imports shared customer schema
In shared/schemas/customer.schema.js:
import mongoose from "mongoose"; export const CustomerSchema = new mongoose.Schema({ // Full Customer schema definition from your main service name: { type: String, required: true }, email: { type: String, required: true, unique: true }, // Add other fields as needed }, { collection: "customers" }); export const CustomerModel = mongoose.model("Customer", CustomerSchema);
In your referral microservice, import and use it:
import { CustomerModel } from '../shared/schemas/customer.schema.js'; import RefModel from './referral.model.js'; // Population now works since the model is registered const referral = await RefModel.findById(referralId).populate(['userID', 'linkedAccounts']);
Solution 3: Manual Population (Alternative)
If you don't want to define the Customer schema in the referral service, you can manually fetch related documents directly from the collection:
const referral = await RefModel.findById(referralId).lean(); // Fetch the main user document const user = await mongoose.connection.collection('customers').findOne({ _id: referral.userID }); referral.userID = user; // Fetch linked accounts const linkedUsers = await mongoose.connection.collection('customers') .find({ _id: { $in: referral.linkedAccounts } }) .toArray(); referral.linkedAccounts = linkedUsers;
- The
refvalue in your Referral schema must match the model name you register in the referral service (not the collection name). Keepref: "Customer"if you register the model as "Customer". - Always explicitly set the
collectionoption in the Customer schema to ensure it matches the collection name used in the main service.
内容的提问来源于stack exchange,提问作者roverrr

