Home > Back-end >  TypeError: Organizations.find is not a function
TypeError: Organizations.find is not a function

Time:01-04

TypeError: Organizations.find is not a function at C:\Users\dillo\Desktop\UWI\ECNG 3020 Final Year Project\Damage_Assessment _Tool\routers\OrganizationsRouters.js:11:23

I can't seem to catch whats wrong. I am separating my routes into differnet files for simplicity but I keep encountering this error "TypeError: Organizations.find is not a function"

The route file contains the route that is needed: OrganizationsRouter.js

const express = require('express')
const router = new express.Router();
const { mongoose } = require ('../db/mongoose');

const Organizations = require('../db/models/Organizations.model')

//Routes for Organizations
router.get('/Organizations', (req,res) => {
    //return an array of all the damage assessments made that is stored on the database.
    Organizations.find({}).then((organization) => {
        res.send(organization);
    }).catch((e) => {
        res.send(e);
    });
})
router.post('/Organizations', (req,res) => {
    //create a damage assessment report and save to the database
    let organizationName = req.body.organizationName;
    let newOrganization = new Organizations({
        organizationName
    });
    newOrganization.save().then((OrganizationDoc) => {
        //the full Organization document is returned (including id)
        res.send(OrganizationDoc);
    })
});
router.patch('/Organizations/:id', (req,res) => {
    //update the Organization specified
    Organizations.findOneAndUpdate({_id: req.params.id},{
        $set: req.body
    }).then(() => {
        res.send({'message' : "Updated Successfully"});
    });
});
router.delete('/Organizations/:id', (req,res) => {
    //delete the Organization specified
    Organizations.findOneAndRemove({
        _id: req.params.id
    }).then((removeOrganizationDoc) => {
        res.send(removeOrganizationDoc);
    })
});

module.exports = router

App.js is the main index file: App.js

const express = require('express');
const app = express();
const { mongoose } = require ('./db/mongoose');
const bodyParser = require ('body-parser');

const res = require('express/lib/response');

/*Load Middleware*/
app.use(bodyParser.json());

//Import Routers
const organizationRouter = require('./routers/OrganizationsRouters')

//Register Router
app.use(organizationRouter)


//Listening to the server on port 3000
app.listen(3000,()=>{
    console.log("Listening to port 3000");
})

Organizations.model.js is the mongoose schema: Organizations.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}

CodePudding user response:

I guess there is an issue in these two lines

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

There should be mongoose.createConnection call at some point as well as connection.model('Organisations', '../db/models/Organizations.model') And then

router.get('/Organizations', (req,res) => {
    Organizations.find(
        {"name": "blah"},
        (err, docs) => {
            if(err) {
                res.send(err)
            } else {
                res.send(docs)
            }
        }
    )
})

CodePudding user response:

Going to run my debugging in written form..

TypeError: Organizations.find is not a function

so what is Organizations...

const Organizations = require('../db/models/Organizations.model')

its labelled a model... do you need to create an instance of it? Or use an ORM tool to do the find rather than call find on a model? What is Organizations.

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

... ok so we have mongoose... i've not used that before, assuming its an orm or some package rather than function... it is... so do we need / have a connection to it? I think not - so maybe we need to create a connection?

https://mongoosejs.com/docs/connections.html

so something like this with your details.

mongoose.connect('mongodb://localhost:27017/myapp');

CodePudding user response:

Try to change the import of the model in OrganizationsRouter.js to:

const { Organizations } = require('../db/models/Organizations.model')
  •  Tags:  
  • Related