如何在mongoose中引用不在项目中的模型

huangapple go评论65阅读模式
英文:

How to reference a model that's not in project using mongoose

问题

I am building a microservice for Referral System for this existing app which has it's main backend already running. This Microservice will be an add-on to that. I built a Referral Model which is like this:

import mongoose from "mongoose";

const RefSchema = new mongoose.Schema({
    referralID: {
        type: String,
        required: true
    },
    userID: {
        type: mongoose.Types.ObjectId,
        ref: 'Customer',
        required: true
    },
    userStatus: {
        type: String,
        required: true
    },
    linkedAccounts: {
        type: [mongoose.Types.ObjectId],
        ref: 'Customer',
        default: []
    }
}, {
    timestamps: true
});

const RefModel = mongoose.model('referrals', RefSchema);
export default RefModel;

Now, As you can see the ref is set to "Customer", which is exactly how it's modeled in the Main server. When I try to populate anything in the referral objects, I get an error from NestJS which says

Schema hasn't been registered for model "Customer". Use mongoose.model(name, schema)

If you have any idea what's gone wrong please let me know, also I looked in the compass, the collection name is "customers", so even by trying that I got the same error.

英文:

I am building a microservice for Referral System for this existing app which has it's main backend already running. This Microservice will be an add-on to that. I built a Referral Model which is like this :

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

import mongoose from &quot;mongoose&quot;;

const RefSchema = new mongoose.Schema({
    referralID : {
        type : String,
        required : true
    },
    userID : {
        type : mongoose.Types.ObjectId,
        ref : &#39;Customer&#39;,
        required : true
    },
    userStatus : {
        type : String,
        required : true
    },
    linkedAccounts : {
        type : [mongoose.Types.ObjectId],
        ref : &#39;Customer&#39;,
        default : []
    }
},{
    timestamps : true
});

const RefModel = mongoose.model(&#39;referrals&#39;, RefSchema);
export default RefModel;

<!-- end snippet -->

Now, As you can see the ref is set to "Customer", which is exactly how it's modelled in the Main server. When I try to populate anything in the referral objects, I get an error from NestJS which says

Schema hasn't been registered for model "Customer".
Use mongoose.model(name, schema)

If you have any idea what's gone wrong please let me know, also I looked in the compass, the collection name is "customers", so even by trying that I got the same error.

答案1

得分: 0

尝试在定义Ref模式之前导入Customer模式,这将初始化所需的模型。

此外,你应该在模式声明中使用mongoose.Schema.Types.ObjectId

import mongoose from "mongoose";
import Customer from "path/to/customer-schema";

const RefSchema = new mongoose.Schema({
    referralID: {
        type: String,
        required: true
    },
    userID: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Customer',
        required: true
    },
    userStatus: {
        type: String,
        required: true
    },
    linkedAccounts: {
        type: [mongoose.Schema.Types.ObjectId],
        ref: 'Customer',
        default: []
    }
}, {
    timestamps: true
});
英文:

Try to import the Customer schema before defining the Ref schema, this will initialize the required model.

Also, you should use mongoose.Schema.Types.ObjectId in Schema declarations:

import mongoose from &quot;mongoose&quot;;
import Customer from &quot;path/to/customer-schema&quot;;

const RefSchema = new mongoose.Schema({
    referralID : {
        type : String,
        required : true
    },
    userID : {
        type : mongoose.Schema.Types.ObjectId,
        ref : &#39;Customer&#39;,
        required : true
    },
    userStatus : {
        type : String,
        required : true
    },
    linkedAccounts : {
        type : [mongoose.Schema.Types.ObjectId],
        ref : &#39;Customer&#39;,
        default : []
    }
},{
    timestamps : true
});

</details>



huangapple
  • 本文由 发表于 2023年6月13日 13:57:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76462030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定