Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot read property 'where' of undefined #118

Closed
martincarre opened this issue May 16, 2020 · 2 comments · Fixed by #136
Closed

Cannot read property 'where' of undefined #118

martincarre opened this issue May 16, 2020 · 2 comments · Fixed by #136

Comments

@martincarre
Copy link

Hey!
I'm not sure it's reasonable to post this here but I got a weird error for which I can't seem to find any solution:

Here's my model:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var uniqueValidator = require('mongoose-unique-validator');

var EquipmentSchema = new Schema({
    model: { type: String, required: true, unique: true },
    price: { type: Number, required: true },
    comments: { type: String },
    market: { type: mongoose.Types.ObjectId, ref: 'Market', required: true },
    category: { type: mongoose.Types.ObjectId, ref: 'Category', required: true },
    make: { type: mongoose.Types.ObjectId, ref: 'Make', required: true },
    rv: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Rv'}],
});

EquipmentSchema.plugin(uniqueValidator);
module.exports = mongoose.model('Equipments', EquipmentSchema);

but when I try to save a new equipment:

var equipmentSaver = function(equipment) {
  return new Promise((resolve, reject) => {
    // console.log(equipment);
    const equipmentToSave = new Equipments({
      _id: equipment.type.model._id,
      market: equipment.type.market._id,
      category: equipment.type.category._id,
      make: equipment.type.make._id,
      model: equipment.type.model.name,
      price: equipment.economicDetails.price,
      comments: equipment.economicDetails.comments,
    });
    console.log(equipmentToSave);
    equipmentToSave.save()
      .then(result => resolve(result))
      .catch(error => reject(error))
   });
}

I get the following error:

Cannot read property 'where' of undefined

for both the model path and the _id path
When I delete the EquipmentSchema.plugin(uniqueValidator); the model gets saved correctly again.
What's weird is that mongoose-unique-validator works like a charm for my user validation...

I've created a question on SO about this here: https://stackoverflow.com/questions/61802039/mongoose-cannot-read-property-where-of-undefined-id

@martincarre
Copy link
Author

Hey everyone!

@mousto090 seem to have found the origin of the problem. I'm calling one of my model field: model which seems to confuse your code in this section .
You can read his answer to my question on SO there.

All the best!

@jhaaken
Copy link
Contributor

jhaaken commented May 13, 2022

I just ran into this, and was causing me a headache but I think I have a fix, which is to add an else to get the model from the constructor as the default case

                            let model;
                            if (isQuery) {
                                model = this.model;
                            } else if (isSubdocument) {
                                model = this.ownerDocument().model(this.ownerDocument().constructor.modelName);
                            } else if (isFunc(this.model)) {
                                model = this.model(this.constructor.modelName);
 ==>                     } else {
 ==>                        model = this.constructor.model(this.constructor.modelName);
 ==>                    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants