has no method 'find'""> has no method 'find'"">

ATM: "Object #<Object> has no method 'find'"

Holy smokes, Bat-viewers! Today’s adventure took me down a rabbit-hole and then up out of an ant-hole.

So our focus this week has been on databases. Coolness - the stacks are really starting to feel complete. Today was our second (and final) day with Mongo. Our task for the day was to complete a functional backend and then modularize it. I struggled with some of the syntax to get the backend up and running, but lunch and asking the right people the right questions helped and I was finally up and running. Then I started to modularize my code. I kept getting informative error after informative error. I went through about a dozen silly little things in about five minutes. And then — brick wall! After staring helplessly at the error, not really understanding how I could get such an error, I sought our instructor’s assistance. Unfortunately, it was right at the end of class and he had to leave. So I was stuck with it.

Symptoms

Code functioned as expected when I had it in a single file, but when I broke it out into modules I got …

1
TypeError: Object #<Object> has no method 'find'

with a reference line to a line like …

1
MongooseSchema.find(function (err, data) {

Mind you, the find function had worked fine until I modularized my code.

Mistake

All of my instructor’s and my googling skills kept turning up discussions, explanations, and examples of static versus instant methods in mongoose and mongodb [1].

Turns out, it was a singular versus plural problem. In my MongooseSchemaModule.js file I had used …

1
module.export = MongooseSchema; // Of course the object had no method find - it had none of the module's code!

instead of …

1
module.exports = MongooseSchema; // Life is so much better with exports!