Route.get () requires callback functions, but got an “Undefined object”,

I am learning to make the Todo app. On a website I learn this https://coderwall.com/p/4gzjqw/build-a-javascript-todo-app-with-express-jade-and-mongodb

I typed as the instruction describes

[app.js]
var main = require('./routes/main');
var todo = require('./routes/todo');
var todoRouter = express.Router();
app.use('/todos', todoRouter);
app.get('/', main.index);
todoRouter.get('/',todo.all);
todoRouter.post('/create', todo.create);
todoRouter.post('/destroy/:id', todo.destroy);
todoRouter.post('/edit/:id', todo.edit);

[/routes/todo.js]
module.exports ={
  all: function(req, res){
    res.send('All todos');
  },
  viewOne: function(req, res){
    console.log('Viewing '+req.params.id);
  },
  create: function(req, res){
    console.log('Todo created');
  },
  destroy: function(req, res){
    console.log('Todo deleted');
  },
  edit: function(req, res){
    console.log('Todo '+req.params.id+' updated');
  }
};

and i got this error message

Error: Route.get () requires a callback function, but received [Undefined object]

Am I missing something here?

+21
source share
8 answers

The textbook todo.allreturns an object callback. This is required for syntax router.get.

From the documentation:

router.METHOD (path callback, [callback, ...])

router.METHOD() Express, METHOD HTTP, GET, PUT, POST .., . , router.get(), router.post(), router.put() ..

- callback todo, callback router.

, todo.js callback ( , todo.all):

module.exports = {
    all: function(req, res){
        res.send('All todos')
    },
    viewOne: function(req, res){
        console.log('Viewing ' + req.params.id);
    },
    create: function(req, res){
        console.log('Todo created')
    },
    destroy: function(req, res){
        console.log('Todo deleted')
    },
    edit: function(req, res){
        console.log('Todo ' + req.params.id + ' updated')
    }
};
+18

:

app.get('/', main.index);
todoRouter.get('/',todo.all);

: Route.get() , [ Undefined] , route.get . todo.all todo.js, main.index. , main.index .

+4

,

yourFile.js:

exports.yourFunction = function(a,b){
  //your code
}

app.js

var express = require('express');
var app = express();
var yourModule = require('yourFile');
app.get('/your_path', yourModule.yourFunction);

, , . xxxx

+2

. .

module.exports = router;
+2

node js 4

express = require('express');
var router = express.Router();

module.exports = router;

+1

, , :

module.exports = () => {
    const method = async (req, res) => {
    }
    return {
        method
    }
}

:

const main = require('./module');

const main = require('./module')();
0

This also happened with my code, but I solved my problem. I checked my routes folder (where all my endpoints are of them). I would recommend you check the route folder file and see if you forgot to add a specific link to the router.

0
source

In my case, I tried to "get" from the express application. Instead, I had to do SET.

app.set('view engine','pug');
0
source

All Articles