【问题标题】:Keep getting this error: Error: Route.post() requires callback functions but got a [object Undefined]不断收到此错误:错误:Route.post() 需要回调函数,但得到了 [object Undefined]
【发布时间】:2018-02-22 19:37:02
【问题描述】:

我正在尝试设置一个以 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 检查数据库中的内容以及用户模式是否存在。如果我在路线页面中注释掉那条路线,错误就会消失。

【问题讨论】:

  • 这是一个简单的错误,因为您刚刚忘记导入某些内容,请随时删除问题。

标签: express model-view-controller routes controllers


【解决方案1】:

我相信问题出在这里:

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

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

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

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

【讨论】:

  • 哇.....真是个愚蠢的错误 smh lol 我已经编码了这么多小时。感谢指出!我什至没有注意到我忘记了要求。
猜你喜欢
  • 2015-12-01
  • 1970-01-01
  • 2020-12-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多