我需要一点帮助,我有一个类别模式,如下所示,

let mongoose = require("mongoose");
let schema = mongoose.Schema;
const _ = require("lodash");

let category = new schema(
  {
    // Primary Fields
    categoryName: { type: String, unique: true },
    isArchived: { type: Boolean, default: false },
    isDeleted: { type: Boolean, default: false },
    categoryImage: { type: String },
    categorySlug: { type: String, unique: true },
    createdBy:{
        type: mongoose.Types.ObjectId,
        ref: "Users",
    }
  },
  {
    timestamps: true,
  }
);

let Category = mongoose.model("Categories", category);
module.exports = {
    Category,
};

我正在将此引用用于另一个模式,如下所示

let mongoose = require("mongoose");
let schema = mongoose.Schema;
const { ObjectID } = require("mongodb");

let coupons = new schema({
    city: {type:String},
    categoryId: {
        type: mongoose.Types.ObjectId,
        ref: "categories",
    },
    offerCode: {type:String,unique:true},
    couponName: {type:String,unique:true},
    validFrom: {type:Date},
    validTill: {type:Date},
    visibility: { type:Boolean,default:false},
    issuedFor: {type:String,enum:['BIKE','AUTO','DELIVERY','CHARGING']},
    applicability: {type:String,enum:['1','2','3','4','5','6','7','8','9','10','0']},
    bankType: {type:String},
    isArchived: { type: Boolean, default: false },
    isDeleted: { type: Boolean, default: false },
    createdBy: {
        type: mongoose.Types.ObjectId,
        ref: "Users",
    },
},
{
    timestamps: true,
});
    coupons.index( { "offerCode": 1,"couponName": 1,"validFrom": 1, "validTill":1, "categoryId":1 }, { unique: true } );
    let Coupons = mongoose.model("Coupons", coupons);
    module.exports = {
         Coupons
    };

一切看起来都很好,我可以使用其他模式的引用,但每当我试图从我的代码填充它时,类别模式就会抛出MongooseError.

代码如下所示,

return await Coupons.find(query)
        .populate('categoryId')
        .skip(parseInt(skip)).select(select).limit(parseInt(limit)).sort({ createdAt: -1 })

有没有人能告诉我是什么原因让我迈向MongooseError [MissingSchemaError]: Schema hasn't been registered for model "categories".

请帮帮我.先谢谢你.

推荐答案

正确的格式应为:

categoryId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: "Category",
    },

您还需要更改模型的名称:

let Category = mongoose.model("Category", category);

Mongoose将查找型号名称的细节化小写集合,因此Mongoose假定为Category->;categories.

第一个参数是模型所针对的集合的单数名称.Mongoose会自动查找您的型号名称的复数小写版本.因此,对于上面的示例,模型Tank用于数据库中的TANKS集合.

然后,在进行填充时,您总是可以显式指定pathmodel,如下所示

return await Coupons.find(query)
        .populate({path: 'categoryId', model: Category})
        .skip(parseInt(skip)).select(select).limit(parseInt(limit)).sort({ createdAt: -1 })

有关详细说明,请参阅Compiling your first modelSaving refs.

Node.js相关问答推荐

需要关于基于角色授权的设计建议

CloudTasksClient在Firebase Function % s onDocumentCreated中实例化时导致错误

购物车是空的状态|本地存储不更新产品数量|Redux|

FindOne()返回所有值,而不是一个值

Nestjs swc 错误:找不到模块项目路径/src/app.module

Postgressql的BIGSERIAL自增序列,即使由于唯一约束错误没有创建行,也会自动增加

Mongoose post中间件触发deleteMany

如何使用 $PATH 变量在系统中全局自动安装 bash 脚本?或者重写脚本到node

Fly.io 启动问题:无法从源获取图像或构建错误:构建错误:连接期间出错:发布...

在 React 和 Socket.io 中使用 Effect 钩子重新渲染两次

多个 Axios 请求合并

Sharp JS 依赖关系 destruct 了 Elastic Beanstalk 上的 Express Server

如何为一个网站实现这 2 个网址.即 www.administrator.sitename.com 和 www.sitename.com?

为什么我在生产环境中 deproy Next.js 示例项目时 CSS 不起作用?

node.js 变量不存在代码块

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

MongoDB Node findone如何处理没有结果?

将 NodeJS 用于大型项目

使用 Node-sass 缩小 CSS

NodeJS:如何调试检测到 EventEmitter 内存泄漏.添加了 11 个侦听器