我正在为这个现有的应用程序建立一个转诊系统的微服务,它的主要后端已经运行.这项微服务将是该服务的一个附加组件.我构建了一个参照模型,如下所示:

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;

现在,如您所见,ref被设置为"Customer",这正是它在主服务器中建模的方式.当我试图填充引用对象中的任何内容时,我从NestJS得到一个错误,它说

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

如果你知道哪里出了问题,请让我知道,我也查了一下指南针,Collection 名是"Customers",所以即使try 了,我也得到了同样的错误.

推荐答案

在定义Ref模式之前try 导入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
});

Node.js相关问答推荐

node 模块错误:类型参数OT具有循环约束''

错误:找不到模块';/var/apps/前端/bash';

Mongoose查询-如何根据当前查找ID获取其他集合并将其插入到当前查找中?

使用Node.js中的";类型";:";模块";不导入文件

是否可以在MongoDB中比较和匹配引用文档中的字段

Node.js分页返回空数组

"sh:/usr/local/bin/node:当它存在于文件目录中时未找到

如何在 ElectronJS 中播放音频文件

FHIR 服务器:尽管 JSON 格式正确,但在 POST 请求中接收未定义请求正文

具有项目外部子路径导入的 Firebase 函数

在 Header 中使用 Authorization 方法和新的附加参数:accessToken 有什么区别?

如何使用来自前一阶段的另一个数组的聚合mongoose 在数组中添加字段

aws cdk 2.0 init 应用程序无法构建更漂亮的问题,这来自 jest-snapshot

无服务器无法获取所有记录事件对象验证失败?

制作一个接受命令行参数的脚本

node 应用程序是否有任何独立的 gui 模块

从 JavaScript 文件或 REPL 中 require()'ing CoffeeScript 文件

Nodejs将字符串转换为UTF-8

Puppeteer 等待所有图像加载然后截图

deno vs ts-node:有什么区别