Ben Wong
Ben Wong

Reputation: 701

Error: Route.put() requires callback functions but got a [object Undefined]

My terminal console is giving me the following:

  Error: Route.put() requires callback functions but got a [object Undefined]

This is my app > routes > articles.server.routes.js

var users = require('../../app/controllers/users.server.controller'),
        articles = require('../../app/controllers/articles.server.controller');

module.exports = function(app) {
    app.route('/api/articles')
        .get(articles.list)
        .post(users.requiresLogin, articles.create);

    app.route('/api/articles/:articleId')
        .get(articles.read)
        // .put(users.requiresLogin, articles.hasAuthorization, articles.update)
        // .delete(users.requiresLogin, articles.hasAuthorization, articles.delete);
        app.param('articleId', articles.articleByID);


};

I am getting error for the following:

// .put(users.requiresLogin, articles.hasAuthorization, articles.update)
// .delete(users.requiresLogin, articles.hasAuthorization, 

When I comment these 2 lines out, the errors in the console go away.

I have "method-override": "~2.2.0" in my package.json and it's properly installed. And I properly declared it in my express.js file so I'm unsure what I did wrong.

var config = require('./config'),
    express = require('express'),
    morgan = require('morgan'), 
    compress = require('compression'),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override'),****
    session = require('express-session'),
    flash = require('connect-flash'),
    passport = require('passport');

module.exports = function(){
    var app = express();

    if (process.env.NODE_ENV === 'development'){
        app.use(morgan('dev'));
    } else if (process.env.NODE_ENV === 'production'){
        app.use(compress());
    }

    app.use(bodyParser.urlencoded({
        extended: true
    }));

    app.use(bodyParser.json());

    app.use(methodOverride());

Upvotes: 2

Views: 5129

Answers (3)

Abayomi Ogunnusi
Abayomi Ogunnusi

Reputation: 21

In my case this is what I did. Instead of importing my controller like this:

const forgotPassword  = require('../controllers/forgotPasswordController');

I used this:

const { forgotPassword } = require('../controllers/forgotPasswordController');

It solved the problem

Upvotes: 2

shdr
shdr

Reputation: 966

That happens when the parameters in api function signature not applicable with the params it expected in compilation time. Make sure your controller imported correctly, and also your contriller action (the function) written in the same at api and cntroller (copy paste).

Upvotes: 0

metalkat
metalkat

Reputation: 620

In case others come across this: when this happened to me, I had mismatching calls, i.e. a PUT was calling a POST. Make sure everything lines up!

Upvotes: -1

Related Questions