我正在尝试使用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检查数据库中的什么,用户架构就在那里。如果我在路线页面中注释掉一条路线,那么错误就会消失。
答案 0 :(得分:2)
我认为问题在于:
const authController = '../controllers/auth_controller';
authRoutes.post('/',authController.signup)
请注意,authController
只是一个字符串。我猜你的意图是:
const authController = require('../controllers/auth_controller');