Salman
Salman

Reputation: 309

TypeError: Cannot read property 'Symbol(mongoose#Document#scope)' of undefined

This the error:

collection is a reserved schema pathname and may break some functionality. You are allowed to use it, but use at your own risk. To disable this warning pass supressReservedKeysWarning as a schema option. /Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174 this.$set.call(this.$__[scopeSymbol] || this, path, v); ^

TypeError: Cannot read property 'Symbol(mongoose#Document#scope)' of undefined at Model.set [as collection] (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/helpers/document/compile.js:174:32) at Function.compile (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/model.js:4801:30) at Mongoose._model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/index.js:551:27) at Mongoose.model (/Users/sama/Desktop/spbackend/node_modules/mongoose/lib/index.js:509:27) at Object. (/Users/sama/Desktop/spbackend/models/product.js:68:28) at Module._compile (internal/modules/cjs/loader.js:1063:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10) at Module.load (internal/modules/cjs/loader.js:928:32) at Function.Module._load (internal/modules/cjs/loader.js:769:14) at Module.require (internal/modules/cjs/loader.js:952:19) [nodemon] app crashed - waiting for file changes before starting...

in product model:

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);

in collection model:

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);

Upvotes: 7

Views: 4415

Answers (2)

Anthony Saldana Jr.
Anthony Saldana Jr.

Reputation: 180

collection is a reserved keyword. You can see the other reserved keywords here: https://mongoosejs.com/docs/api.html#schema_Schema.reserved

You'll have to rename the collection field to anything else.

Upvotes: 6

Motaz Abuthiab
Motaz Abuthiab

Reputation: 171

You issue is related to collection field in your schema, I had the same issue, it took me time to figure out. See mongo schema reserved fields here: https://mongoosejs.com/docs/api.html#schema_Schema.reserved

To fix the issue, just rename the field to something else:

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

Upvotes: 17

Related Questions