miatech
miatech

Reputation: 2268

Find is not a method Mongoose Error

I'm working with a todoList app using express, node, mongodb, and I keep getting "find is not a method of" in this case my variable is todoItem. I created the schema and have data inside the db. When I run my app a collection is created in mongodb with "todos" name. then I can see the documents inside "todos" But still keep getting the error. Any help appreciated!

todoItem.find({}, function(err, todoItem) {
         ^

TypeError: todoItem.find is not a function
    at Object.<anonymous> (/home/george/Desktop/work/todo-nodejs/app.js:40:10)
    at Module._compile (module.js:573:30)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3

my app.js

var express = require("express");
var app = express();
var bodyParse = require("body-parser");
var mongoose = require("mongoose");

//mongodb connection
mongoose.connect("mongodb://localhost/todo");

//view engine for ejs file
app.set("view engine", "ejs");

//bodyparser for extracting data form ejs pages
app.use(bodyParse.urlencoded({extended: true}));


//mogoose schema
var todoSchema = new mongoose.Schema({
    name: String
});

var todo = mongoose.model("Todo", todoSchema);

//adding a new todoItem to db
var todoItem = new todo({
    name: "Wash car and change oil!"
});

//saving todoItem to mongodb
// todoItem.save(function(err, todoItem) {
//     if(err) {
//         console.log(err);
//     }
//     else {
//         console.log("saving: "+todoItem);
//     }
// });

//retrieving todoItem from mongodb
todoItem.find({}, function(err, todoItem) {
    if(err) {
        console.log("something went wrong");
    }
    else {
        console.log("find data: "+todoItem);
    }
});


/*var todoList = [
    "print documents for driving test",
    "send resume to prospect employers",
    "buy food for the pigs and shower"
]*/


//======Express routes Here!============//

//default route
app.get("/", function(req, res) {
    res.render("index.ejs", {todoList: todoList});
});

//add new todo item to list
app.post("/newtodo", function(req, res) {
    console.log(req.body.todoitem);
    var item = req.body.todoitem;
    todoList.push(item);
    res.redirect("/");
});


//catch all other routes
app.get("*", function(req, res) {
    res.send("all others /*");
});


//express server
app.listen(3000, function() {
    console.log("Server started port 3000");
});

Upvotes: 0

Views: 34

Answers (1)

Timo
Timo

Reputation: 738

Try todo.find(), because your model is stored into todo variable. todoItem is an instance of this model.

Upvotes: 1

Related Questions