【问题标题】:TypeError: Cannot read property 'Symbol(mongoose#Document#scope)' of undefinedTypeError:无法读取未定义的属性'Symbol(mongoose#Document#scope)'
【发布时间】:2021-09-25 01:42:11
【问题描述】:

这是错误:

collection 是保留的模式路径名,可能会破坏某些功能。您可以使用它,但使用风险自负。要禁用此警告,请将 supressReservedKeysWarning 作为模式选项传递。 /Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174 this.$set.call(this.$__[scopeSymbol] || this, path, v); ^

TypeError: 无法读取未定义的属性 'Symbol(mongoose#Document#scope)' 在 Model.set [作为集合] (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174:32) 在 Function.compile (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/model.js:4801:30) 在 Mongoose._model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/index.js:551:27) 在 Mongoose.model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/index.js:509:27) 在对象。 (/Users/sama/Desktop/spbackend/models/product.js:68:28) 在 Module._compile (internal/modules/cjs/loader.js:1063:30) 在 Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) 在 Module.load (internal/modules/cjs/loader.js:928:32) 在 Function.Module._load (internal/modules/cjs/loader.js:769:14) 在 Module.require (internal/modules/cjs/loader.js:952:19) [nodemon] 应用程序崩溃 - 启动前等待文件更改...

在产品型号中:

const mongoose = require('mongoose');

const productSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    description: {
        type: String,
        required: true
    },
    richDescription: {
        type: String,
        default: ''
    },
    image: {
        type: String,
        default: ''
    },
    images: [{
        type: String,
    }],
    brand: {
        type: String,
        default: ''
    },
    price: {
        type: Number,
        default: 0
    },
    countInStock: {
        type: Number,
        required: true,
        min: 0,
        max: 255
    },
    rating: {
        type: Number,
        default: 0
    },
    numReviews: {
        type: Number,
        default: 0
    },
    isFeatured: {
        type: Boolean,
        default: false
    },
    dateCreated: {
        type: Date,
        default: Date.now
    },
    collection: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'Collection'
    }
}
)

productSchema.virtual('id').get(function () {
    return this._id.toHexString();
});

productSchema.set('toJSON', {
    virtuals: true,
});

exports.Product = mongoose.model('Product', productSchema);

在集合模型中:

const mongoose = require('mongoose');

const collectionSchema = mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    image: {
        type: String,
        required: true
    }
})

collectionSchema.virtual('id').get(function () {
    return this._id.toHexString();
});

collectionSchema.set('toJSON', {
    virtuals: true,
});

exports.Collection = mongoose.model('Collection', collectionSchema);

【问题讨论】:

    标签: node.js typescript mongoose-schema


    【解决方案1】:

    您的问题与架构中的 collection 字段有关,我遇到了同样的问题,我花了一些时间才弄清楚。在此处查看 mongo 架构保留字段:https://mongoosejs.com/docs/api.html#schema_Schema.reserved

    要解决此问题,只需将该字段重命名为其他名称:

    collectionName: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'Collection'
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-20
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 2017-11-03
      • 1970-01-01
      相关资源
      最近更新 更多