Thursday, May 9, 2024
 Popular · Latest · Hot · Upcoming
138
rated 0 times [  144] [ 6]  / answers: 1 / hits: 7409  / 2 Years ago, tue, december 28, 2021, 12:00:00

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
}

More From » node.js

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

[#551] Friday, December 17, 2021, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
byrondonavanc

Total Points: 675
Total Questions: 107
Total Answers: 105

Location: Peru
Member since Fri, Oct 14, 2022
2 Years ago
byrondonavanc questions
;