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

In the tutorial the todo.all returns a callback object. This is required for the router.get syntax.

From the documentation:

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

The router.METHOD() methods provide the routing functionality in Express, where METHOD is one of the HTTP methods, such as GET, PUT, POST, and so on, in lowercase. Thus, the actual methods are router.get(), router.post(), router.put(), and so on.

You still need to define the array of callback objects in your todo files so you can access the proper callback object for your router.

You can see from your tutorial that todo.js contains the array of callback objects (this is what you are accessing when you write 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')
    }
};

Leave a Comment