node.js - Cannot read 'create' property of undefined -


i following tutorial on building app express, nodejs, sequelize,postgres. after creating controller , routes, route works post route (mean call callback function creating object) fails with:

typeerror: cannot read property 'create' of undefined.

this controller:

let todo = require('../models').todo;  module.exports = {     create(req, res){         return todo         .create({title:req.body.title,})         .then(todo => res.status(201).send(todo))         .catch(error => res.status(400).send(error));     },     list(req, res){         return todo         .all()         .then(todos => res.status(200).send(todos))         .catch(error => res.status(400).send(error));     }, }; 

and route definition

const todoscontroller = require('../controllers').todos;   module.exports = app => {     app.get('/api', (req, res) =>res.status(200).send({message:"welcome todos api!"}));     app.post('/api/todos', todoscontroller.create);     app.get('/api/todos', todoscontroller.list); }; 

...and here todo model. @yuri tarabanko

'use strict';    module.exports = (sequelize, datatypes) =>{    const todo = sequelize.define('todo', {      title:{         type:datatypes.string,        allownull: false      },    },     {      classmethods: {        associate: (models) => {          todo.hasmany(models.todoitem,            {            foreignkey:'todoid',            as: 'todoitems',                          });            },        },      }      );    return todo;  };

my controllers/index.js file shown below.

const todos = require('./todos');  module.exports = {      todos,  };

should not

const todoscontroller = require('../controllers').todos; 

but

const todoscontroller = require('../controllers'); 

because create , list functions not in todo in root of module.exports.

create , list functions in "todo.js" file in "controllers" directory.

then should require them as

const todoscontroller = require('../controllers/todo'); 

unless have controllers/index.js has

modules.exports={todo:require(todo)}; 

also note file seems called todo, not todos.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -