2

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.

Community
  • 1
  • 1
sandr
  • 21
  • 6
  • I would guess you have a misspelling somewhere that is failing to match the property or module name. I don't see it in the code you've shown, but that's what you should check. You can load the whole module: `const x = require('./institution')` and then `console.log(x)` to see if the expected properties are there. – jfriend00 Feb 26 '17 at 17:11
  • Thanks for the suggestion. If I require the module as a whole `const x = require('./institution')` and log it, it returns an empty object `{}`. I have checked the spelling. I even copied it directly from a module where it works. Atom highlights all uses of it and it matches spelling on everything across the file. – sandr Feb 26 '17 at 17:55
  • I think you need to recheck your file system. You may have an empty `institution.js` that is somehow being loaded from one place, but not another. Are all your files in the same directory of do you have a hierarchy. If it's not misspelled, the only logical explanation is that your `require('./institution')` is not finding the right file. – jfriend00 Feb 26 '17 at 18:00
  • 1
    That makes absolute sense, but it doesn't seem to be the issue. This is baffling. I required `institution.js` from other directories pointing to the file in the models directory. They work fine. It doesn't have a problem until I point to it in the same directory from another file in models. However, I can reach `student.js` in the same directory just fine. I tried setting up a test function in `student.js` to reach `institution.js` and it returns an empty object there also! – sandr Feb 26 '17 at 18:26
  • You do understand that the `./` at the front of the path means to load it ONLY from the same directory as the current module is loaded from, right? Are `student.js` and `institution.js` in the same directory? – jfriend00 Feb 26 '17 at 18:28
  • Yes, I understand that. They are in the same directory, as is the file I am loading them into. When I'm reaching it from another directory, I use `require('../models/institution')`. – sandr Feb 26 '17 at 18:33
  • One more idea. Is there a subfolder named `institution` in the same directory as `institution.js` is located or a file named `institution.json`? If so, change your statement to `require('./institution.js')`. You can see the loading logic for `require(x)` [here](https://nodejs.org/api/modules.html#modules_all_together). FYI, I always use `.js` suffixes on my `require()` statements to unambiguously specify that I want the `x.js` file loaded. – jfriend00 Feb 26 '17 at 18:44
  • 1
    Thanks jfriend00 for your help. It led me to rephrase my question, which led me to a StackOverflow question from 2 years ago about a "circular dependency": http://stackoverflow.com/questions/23875233/require-returns-an-empty-object/23875299. This explained the problem. – sandr Feb 26 '17 at 23:58
  • Glad you figured it out. I guess the empty object should have been the clue. – jfriend00 Feb 27 '17 at 00:20

0 Answers0