MEANJS TypeError: Route.all() requires callback functions but got a [object Undefined]

MEANJS TypeError: Route.all() requires callback functions but got a [object Undefined]

我目前正在学习 MEAN Stack,并且一直在使用 meanjs.org 4.1 版的 yo 生成器。我在生成的应用程序中包含了 CRUD 示例,并将其用作我自己的代码的示例。

我 运行 遇到的错误是:

     TypeError: Route.all() requires callback functions but got a [object Undefined]
    at Route.all (/Users/username/git/interview-app/node_modules/express/lib/router/route.js:173:13)
    at module.exports (/Users/username/git/interview-app/modules/interviews/server/routes/interviews.server.routes.js:9:32)
    at /Users/username/git/interview-app/config/lib/express.js:183:37
    at Array.forEach (native)
    at Object.module.exports.initModulesServerRoutes (/Users/username/git/interview-app/config/lib/express.js:182:30)
    at Object.module.exports.init (/Users/username/git/interview-app/config/lib/express.js:248:8)
    at /Users/username/git/interview-app/config/lib/app.js:29:23
    at /Users/username/git/interview-app/config/lib/mongoose.js:36:15
    at open (/Users/username/git/interview-app/node_modules/mongoose/lib/connection.js:488:17)
    at NativeConnection.Connection.onOpen (/Users/username/git/interview-app/node_modules/mongoose/lib/connection.js:498:5)
    at /Users/username/git/interview-app/node_modules/mongoose/lib/connection.js:457:10
    at /Users/username/git/interview-app/node_modules/mongoose/lib/drivers/node-mongodb-native/connection.js:60:5
    at /Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb/lib/db.js:229:5
    at connectHandler (/Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb/lib/server.js:279:7)
    at g (events.js:260:16)
    at emitOne (events.js:77:13)
    at emit (events.js:169:7)
    at /Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:409:23
    at /Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:778:13
    at Callbacks.emit (/Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:95:3)
    at null.messageHandler (/Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb-core/lib/topologies/server.js:249:23)
    at Socket.<anonymous> (/Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb-core/lib/connection/connection.js:262:22)
/Users/username/git/interview-app/node_modules/mongoose/node_modules/mongodb/lib/server.js:282
      process.nextTick(function() { throw err; })

这是我的 interviews.server.routes.js:

的代码
    *** interviews.server.routes.js ***

    'use strict';

    // Module dependencies
    var interviewsPolicy = require('../policies/interviews.server.policy'),
        interviews = require('../controllers/interviews.server.controller');s

    module.exports = function (app) {
      //Interviews collection routes
      app.route('/api/interviews').all(interviewsPolicy.isAllowed)
       .get(interviews.list)
       .post(interviews.create);

      // Single Interview routes
      app.route('/api/interviews/:interviewId').all(interviewsPolicy.isAllowed)
      .get(interviews.read)
      .put(interviews.update)
      .delete(interviews.delete);

     // Bind the interview to middleware
     app.param('interviewId', interviews.interviewByID);
   };

这里是 interviews.server.routes.js.

模块依赖的代码

政策代码:

    *** interviews.server.policy.js ***

    'use strict';
    //Module dependencies
    var acl = require('acl');

    // Using the memory backend
    acl = new acl(new acl.memoryBackend());

    // Invoke Interviews Permissions
    exports.invokeRolesPolicies = function () {
      acl.allow([{
        roles: ['admin'],
        allows: [{
          resources: '/api/interviews',
          permissions: '*'
        },
        {
          resources: '/api/interviews/:interviewId',
          permissions: '*'
        }]
      },
      {
        roles: ['user'],
        allows: [{
          resources: '/api/interviews',
          permissions: ['get', 'post']
        },
        {
          resources: '/api/interviews/:interviewId',
          permissions: ['get']
         }]
      },
      {
        roles: ['guest'],
        allows: [{
          resources: '/api/interviews',
          permissions: ['get']
        },
        {
          resources: '/api/interviews/:interviewId',
          permissions: ['get']
        }]  
      }]);
    };

控制器代码:

    'use strict';

// Module dependencies
var path = require('path'),
    mongoose = require('mongoose'),
    Interview = mongoose.model('Interview'),
    errorHandler = require(path.resolve('./modules/core/server/controllers/errors.server.controller'));

// Create an Interview
exports.create = function (req, res) {
  var interview = new Interview(req.body);
  interview.user = req.user;

  interview.save(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(interview);
    }
  });
};

// Show the current Interview
exports.read = function (req, res) {
  res.json(req.interview);
};

// Update Interview
exports.update = function (req, res) {
  var interview = req.interview;

  interview.title = req.body.title;
  interview.question = req.body.question;
  interview.answer = req.body.answer;

  interview.save(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(interview);
    }
  });
};

// Delete Interview
exports.delete = function (req, res) {
  var interview = req.interview;

  interview.remove(function (err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(interview);
    }
  });
};

// List of Interviews
exports.list = function (req, res) {
  Interview.find().sort('-created').populate('user', 'displayName').exec(function (err, interviews) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(interview);
    }
  });
};

// Article middleware
exports.interviewByID = function (req, res, next, id) {

  if (!mongoose.Types.ObjectId.isValid(id)) {
    return res.status(400).send({
      message: 'Interview is invalid'
    });
  }

  Interview.findById(id).populate('user', 'displayName').exec(function (err, interview) {
    if (err) {
      return next(err);
    } else if (!interview) {
      return res.status(400).send({
        message: 'No interview with that identifier has been found'
      });
    }
    req.interview = interview;
    next();
  });
};

在我的 package.json 中,我正在使用: 快递:4.13.1, 猫鼬:4.2.3, 节点:0.12.0

如有任何帮助,我们将不胜感激。请询问您是否需要更多信息。 谢谢 - B

让我们把它缩短一点。您正在导入模块

var interviewsPolicy = require('../policies/interviews.server.policy'),

看起来像

exports.invokeRolesPolicies = function () {
     // stuff
};

然后你

app.route('/api/interviews/:interviewId').all(interviewsPolicy.isAllowed)

但显然模块没有 isAllowed 属性,它唯一的 属性 是 invokeRolesPolicies,这是一个函数,所以你传递 undefinedRoute.all 函数。