JavaScript Building a MEAN Application Setting Up MongoDB Creating Seed Data

Todo.find is not a function

So I am in the middle of creating my seed data and when I save the file nodemon restarts the server and then throws the following error.

C:\Users\jweland.ICS\Documents\projects\mean-todo\src\seed.js:13
        Todo.find({'name': todo}, function(err, todos) {
             ^

TypeError: Todo.find is not a function
    at C:\Users\jweland.ICS\Documents\projects\mean-todo\src\seed.js:13:7
    at Array.forEach (native)
    at Object.<anonymous> (C:\Users\jweland.ICS\Documents\projects\mean-todo\src\seed.js:12:7)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Module.require (module.js:367:17)
    at require (internal/module.js:16:19)
    at Object.<anonymous> (C:\Users\jweland.ICS\Documents\projects\mean-todo\src\app.js:9:1)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:141:18)
[nodemon] app crashed - waiting for file changes before starting...

my seed.js file is as follows

seed.js
'use strict';

var Todo = require('./models/todo.js');

var todos = [
    'reply to dad\'s tech question email from two weeks ago.',
    'reveal the identity of the left shark',
    'sing karaoke like nobody\'s watching',
    'reply to dad\'s followup email after the first reply'
];

todos.forEach(function(todo, index) {
    Todo.find({'name': todo}, function(err, todos) {
        if (!err && !todos.length) {
            Todo.create({completed: false, name: todo});
        };
    });
});

Maybe its the lack of coffee but I can't wrap my head around why .find would not be a function in this js file but it is and all the others.

Matt F.
Matt F.
9,517 Points

If you are still having issues, post the code from your Todo module and I will take a look.

Thanks Matt F.

todo.js
'use strict';

var mongoose = require('mongoose');

// todo.name

// todo.completed


var todoSchema = mongoose.Schema({
    name: String,
    completed: Boolean
});

var model = mongoose.model('Todo', todoSchema);

model.exports = model;

1 Answer

Matt F.
Matt F.
9,517 Points

Hi John,

I do not currently have a subscription to Treehouse, so I can only offer suggestions based on the way I have utilized Mongo/Node.

One thing that I notice is that there is no connection that is explicitly opened to a Mongo database - but maybe Treehouse has it so you do not have to?

The other potential issue that I noticed is that you do not have the new keyword before mongoose.schema.

var todoSchema = new mongoose.Schema({
    name: String,
    completed: Boolean
});

figured it out

todo.js
model.exports = model;

should be

todo.js
module.exports = model;