继续收到此错误:错误:Route.post()需要回调函数但得到[对象未定义]

时间:2017-09-14 00:12:26

标签: express model-view-controller routes controllers

我正在尝试使用mongo db作为数据库设置react express应用程序。我处于初步阶段并不断遇到此错误:

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

这是我的app.js

const express = require('express');
// const http = require('http');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();
const mongoose = require('mongoose');

mongoose.Promise = global.Promise;


//db  and name is auth

mongoose.connect('mongodb://localhost/auth', {
    useMongoClient: true,
    /* other options */
  });
// app setup

//server setup

const port = process.env.Port || 4000
// const server = http.createServer(app);
app.listen(port);
console.log(`Sever listening on ${port}`)

const authRoutes = require('./routes/auth_routes');
app.use('/',authRoutes); 

我的路线就在这里。我只是在测试是否有正确的连接。

const authController = '../controllers/auth_controller';
const express = require('express');
const authRoutes = express.Router();


    authRoutes.post('/',authController.signup)



module.exports = authRoutes;

我的控制器如下:

const authController = {};

authController.signup = function(req,res,next) {
    console.log('here');
    res.json({
        user: "doesnt matter",
        data: 'Put a user profile on this route'
      });
}

module.exports = authController;

不确定mongo是否是问题,因为它是我第一次使用它,但我与数据库的连接工作robo 3t检查数据库中的什么,用户架构就在那里。如果我在路线页面中注释掉一条路线,那么错误就会消失。

1 个答案:

答案 0 :(得分:2)

我认为问题在于:

const authController = '../controllers/auth_controller';

authRoutes.post('/',authController.signup)

请注意,authController只是一个字符串。我猜你的意图是:

const authController = require('../controllers/auth_controller');