架构模型声明的Mongoose错误

2nbm6dog  于 2023-08-06  发布在  Go
关注(0)|答案(1)|浏览(111)

当我测试模型时,我得到了这个错误:TypeError:无法读取undefined的属性(正在阅读“Symbol(mongoose#Document#scope)”)
我正在做一个电子商务API,其他模式和模型都工作正常,但这个模型抛出了一个错误。而且 Mongoose 中没有保留的“产品”,那么为什么会发生此错误
以下是我的产品型号:

const mongoose = require("mongoose");
const mongoosePaginate = require("mongoose-paginate-v2");
var aggregatePaginate = require("mongoose-aggregate-paginate-v2");

const Product = new mongoose.Schema({
    productId:{
        type:Number,
        required:[true,"Please add a product ID"],
        trim:true,
        unique:true,
    },
    description:{
        type:String,
        required:[true,"Please add a description"],
        trim:true,
    },
    catId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Category",
        required:[true,"Please add a category ID"],
    },
    subCatId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"SubCategory",
        required:[true,"Please add a subcategory ID"],
    },
    brandId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Brand",
        required:[true,"Please add a brand ID"],
    },
    unit:{
        type:String, // Pcs, Kg, Ltr, etc.
        required:[true,"Please add a unit"],
        trim:true,
    },
    purchaseQuantity: {
        min: {
          type: Number,
          default: 1,
        },
        max: {
          type: Number,
        },
      },
    refundable:{
        type:Boolean,
        default:false,
    },
    taxId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Tax",
        default:null,
    },
    collectionId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Collections",
        default:null,
    },
    live:{
        type:Boolean,
        default:false,
    },
    hsnCode:{
        type:String,
        required:[true,"Please add a HSN code"],
        trim:true,
    },
    sellerId:{
        type:mongoose.Schema.Types.ObjectId,
        ref:"Seller",
        required:[true,"Please add a seller ID"],
    },
    sellerUserId:{
        type:Number,
        ref:"Seller",
        required:[true,"Please add a seller user ID"],
    },
    targetGender:{
        type:String,
        enum:['male','female','unisex'],
        required:[true,"Please add a target gender"],
        trim:true,
    },
    productSpecifications:{
        type:Array,
        required:[true,"Please add a product specification"],
        trim:true,
    },
    ageGroup:{
        type:String,
        required:[true,"Please add an age group"],
        trim:true,
    },
    careInstructions:{
        type:String,
        required:[true,"Please add care instructions"],
        trim:true,
    },
},{timestamps:true,toJSON: { virtuals: true }, toObject: { virtuals: true } });

Product.plugin(mongoosePaginate);
Product.plugin(aggregatePaginate);

Product.virtual("category", {
    ref: "Category",
    localField: "catId",
    foreignField: "_id",
    justOne: true,
});

Product.virtual("subcategory", {
    ref: "SubCategory",
    localField: "subCatId",
    foreignField: "_id",
    justOne: true,
});

Product.virtual("brand", {
    ref: "Brand",
    localField: "brandId",
    foreignField: "_id",
    justOne: true,
});

Product.virtual("tax", {
    ref: "Tax",
    localField: "taxId",
    foreignField: "_id",
    justOne: true,
});

Product.virtual("collection", {
    ref: "Collections",
    localField: "collectionId",
    foreignField: "_id",
    justOne: true,
});

Product.virtual("seller", {
    ref: "Seller",
    localField: "sellerId",
    foreignField: "_id",
    justOne: true,

});

Product.virtual("variants", {
    ref: "Variant",
    localField: "productId",
    foreignField: "productId",
    justOne: false,
});



Product.plugin(mongoosePaginate);
Product.plugin(aggregatePaginate);

module.exports = mongoose.model("Product", Product);

字符串
我在这一行遇到了错误:第一个月
错误堆栈:

D:\BackendProjects\node_modules\mongoose\lib\helpers\document\compile.js:205
        this.$set.call(this.$__[scopeSymbol] || this, path, v);
                               ^

TypeError: Cannot read properties of undefined (reading 'Symbol(mongoose#Document#scope)')
    at Model.set [as collection] (D:\BackendProjects\node_modules\mongoose\lib\helpers\document\compile.js:205:32)
    at Function.compile (D:\BackendProjects\node_modules\mongoose\lib\model.js:4733:30)
    at Mongoose._model (D:\BackendProjects\node_modules\mongoose\lib\index.js:620:27)
    at Mongoose.model (D:\BackendProjects\node_modules\mongoose\lib\index.js:579:27)
    at Object.<anonymous> (D:\BackendProjects\src\models\Product Management\productModel.js:182:27) 
    at Module._compile (node:internal/modules/cjs/loader:1218:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
    at Module.load (node:internal/modules/cjs/loader:1081:32)
    at Module._load (node:internal/modules/cjs/loader:922:12)
    at Module.require (node:internal/modules/cjs/loader:1105:19)

Node.js v18.13.0


技术栈:Nodejs express
我试图使用我的模型为我的控制器和路由器,并没有线索,为什么会发生这个错误。我想在我的控制器和路由器中使用该模型,而不更改模型名称“Product”

nhhxz33t

nhhxz33t1#

由于使用Collections模型而发生错误,我将其重命名为ProductCollections,因为我在collectionId字段中使用了Collections的引用,它在Product模型中抛出了一个错误,现在将其更改为ProductCollections解决了错误,因为该集合是mongoose模式的保留字。

相关问题