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

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

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:

  1. import mongoose from "mongoose";
  2. const RefSchema = new mongoose.Schema({
  3. referralID: {
  4. type: String,
  5. required: true
  6. },
  7. userID: {
  8. type: mongoose.Types.ObjectId,
  9. ref: 'Customer',
  10. required: true
  11. },
  12. userStatus: {
  13. type: String,
  14. required: true
  15. },
  16. linkedAccounts: {
  17. type: [mongoose.Types.ObjectId],
  18. ref: 'Customer',
  19. default: []
  20. }
  21. }, {
  22. timestamps: true
  23. });
  24. const RefModel = mongoose.model('referrals', RefSchema);
  25. 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 -->

  1. import mongoose from &quot;mongoose&quot;;
  2. const RefSchema = new mongoose.Schema({
  3. referralID : {
  4. type : String,
  5. required : true
  6. },
  7. userID : {
  8. type : mongoose.Types.ObjectId,
  9. ref : &#39;Customer&#39;,
  10. required : true
  11. },
  12. userStatus : {
  13. type : String,
  14. required : true
  15. },
  16. linkedAccounts : {
  17. type : [mongoose.Types.ObjectId],
  18. ref : &#39;Customer&#39;,
  19. default : []
  20. }
  21. },{
  22. timestamps : true
  23. });
  24. const RefModel = mongoose.model(&#39;referrals&#39;, RefSchema);
  25. 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

  1. import mongoose from "mongoose";
  2. import Customer from "path/to/customer-schema";
  3. const RefSchema = new mongoose.Schema({
  4. referralID: {
  5. type: String,
  6. required: true
  7. },
  8. userID: {
  9. type: mongoose.Schema.Types.ObjectId,
  10. ref: 'Customer',
  11. required: true
  12. },
  13. userStatus: {
  14. type: String,
  15. required: true
  16. },
  17. linkedAccounts: {
  18. type: [mongoose.Schema.Types.ObjectId],
  19. ref: 'Customer',
  20. default: []
  21. }
  22. }, {
  23. timestamps: true
  24. });
英文:

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:

  1. import mongoose from &quot;mongoose&quot;;
  2. import Customer from &quot;path/to/customer-schema&quot;;
  3. const RefSchema = new mongoose.Schema({
  4. referralID : {
  5. type : String,
  6. required : true
  7. },
  8. userID : {
  9. type : mongoose.Schema.Types.ObjectId,
  10. ref : &#39;Customer&#39;,
  11. required : true
  12. },
  13. userStatus : {
  14. type : String,
  15. required : true
  16. },
  17. linkedAccounts : {
  18. type : [mongoose.Schema.Types.ObjectId],
  19. ref : &#39;Customer&#39;,
  20. default : []
  21. }
  22. },{
  23. timestamps : true
  24. });
  25. </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:

确定