Home > Net >  TypeError: Cannot read properties of undefined (reading 'find')
TypeError: Cannot read properties of undefined (reading 'find')

Time:12-31

I'm getting the error, "TypeError: Cannot read properties of undefined (reading 'find')" which points to the block of code:

app.get('/Organizations', (req,res) => {
Organizations.find({}).then((organization) => {
    res.send(organization);
}); })

app.js, importing mongoose schema:

const {Organizations} = require('./db/models');

Organization.model.js:

const mongoose = require('mongoose');

const OrganizationsSchema = new mongoose.Schema({
    organizationName:{
        type: String,
        required: true,
        minlength:1,
        trim: true
    }
})

    
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);

module.exports =  (Organizations)

index.js:

const { Organizations } = require('./Organizations.model');
module.exports = {
    Organizations
}

CodePudding user response:

You added parentheses instead of brackets when exporting Organisations. Try with this :

const mongoose = require('mongoose');

const OrganizationsSchema = new mongoose.Schema({
    organizationName:{
        type: String,
        required: true,
        minlength:1,
        trim: true
    }
})

    
const Organizations = mongoose.model( 'Organizations', OrganizationsSchema);

module.exports =  {Organizations}
  • Related