Solution Found: Turns out the problem was a "circular" dependency. For anyone who else might encounter this problem, I found the answer here: Require returns an empty object
First time asking a question. I'm still learning, and I thought I knew what I was doing here, but I can't make sense of this.
I am working on a Node project with MongoDB/Mongoose. At the top of my file, I have:
const {Institution} = require('./institution');
const {Student} = require('./student');
When I run my program and use Institution.findOne()
it errors and says cannot read property findOne of undefined
. I use the Institution
model in several other files and they all work fine.
The model is required in several steps before this one and always works fine. Since the model works in other cases, I would think the exports are working just fine.
const Institution = database.model('Institution', InstitutionSchema);
module.exports = {
Institution,
InstitutionSchema
};
Both institution.js
and student.js
are in the same folder as this file.
When I do console.log(Student)
, it returns the huge Mongoose object.
When I do console.log(Institution)
, it returns undefined
.
If I console.log(Institution)
from another module where it is working, it returns the Mongoose object
.
The Institution
collection is in my database and I can view the content in Robomongo viewer.
Am I missing something that I just can't see?
Thanks in advance for any help.