admin管理员组

文章数量:1335361

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
}

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
}
Share Improve this question edited Mar 31, 2022 at 13:56 Youssouf Oumar 46.4k16 gold badges102 silver badges105 bronze badges asked Dec 28, 2021 at 9:46 DillonDillon 1542 gold badges6 silver badges15 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 4

It's an import/export problem. In Organization.model.js you used parentheses instead of curly braces when exporting Organisations. Export it like so:

module.exports =  {Organizations}

And import it this way:

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

本文标签: