In one nodeJS (node1) application I created a businessSchema:
import mongoose from "mongoose";
const Schema = mongoose.Schema;
const businessSchema = new Schema({
name: {
type: String,
required: true
}
},
{timestamps: true});
const Businesses = mongoose.model<BusinessModel>("Businesses", businessSchema);
export default Businesses;
In the other nodejs application (node2) I created campaignSchema that has ref to the Businesses schema:
import mongoose from "mongoose";
const Schema = mongoose.Schema;
const campaignSchema = new Schema({
promotion: {
type: String,
required: true
},
business: {
type: Schema.Types.ObjectId,
required: false,
ref: "Businesses"
},
},
{timestamps: true});
const Campaigns = mongoose.model<CampaignModel>("Campaigns", campaignSchema);
export default Campaigns;
when I tried to fetch the campaign (from node2)
async findById(id: string) {
const campaign = Campaigns.findOne({ _id: id })
.populate("business")
.exec();
return campaign;
}
I get this error: MissingSchemaError: Schema hasn't been registered for model "Businesses". Use mongoose.model(name, schema)
This error has not occurred when used monolith (one application) Also, this error obviously has not occurred when removing the populate()
I looked here:https://stackoverflow.com/a/47038544/3083605 which did not help because it is multiple connections from the same application and the models and schema declared in the same connection or process which is not my case