W
W
whiteleaf2017-10-06 19:06:58
MongoDB
whiteleaf, 2017-10-06 19:06:58

Why am I getting the error "Task.findOne is not a function"?

I've been trying to figure out what the problem is here for about an hour, but so far to no avail.
It seems that I do everything as the book writes (mongo documentation), but it gives an error.

TypeError: Task.find is not a function
    at C:\Users\valer\WebstormProjects\todo\server\routes\tasks.js
:7:8

I need to make CRUD and link it to Angular.js.
server.js
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');

var index = require('./server/routes/index');
var tasks = require('./server/routes/tasks');

var port = 3000;

var app = express();

// Connecting to MongoDB
mongoose.connect('mongodb://localhost/todo');

// View engine
app.set('views',path.join(__dirname,'dist'));
app.set('view engine','ejs');
app.engine('html', require('ejs').renderFile);

// Set Static Folder
app.use(express.static(path.join(__dirname, 'client')));

// Body Parser
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use('/', index);
app.use('/api', tasks);

app.listen(port, function(){
  console.log('Server is running on port: '+ port);
});

tasks.js
var express = require('express');
var router = express.Router();
var Task = require('../models/task');


router.get('/tasks', function(req,res,next){
  console.log(Task);
  //Task.find({});
});

module.exports = router;

task.js (Schema)
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var TaskSchema = new Schema({
  title:  String,
  isDone: String
});

var Task = mongoose.model('Task', TaskSchema);

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question