我创建了一个控制器文件来处理表单数据,并用控制器替换了路由文件中的回调函数。但是,只要我运行node js项目,控制器文件中的res.render都会引发错误。 以下是我遇到的错误
/
usr/bin/node /home/jebo/Documents/node_projects/cvbuilder/bin/www
/home/jebo/Documents/node_projects/cvbuilder/controllers/register.js:4
render('user/register',
^
TypeError: render is not a function
at Object.exports.displayRegister (/home/jebo/Documents/node_projects/cvbuilder/controllers/register.js:4:9)
at Object.<anonymous> (/home/jebo/Documents/node_projects/cvbuilder/routes/user.js:21:44)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/home/jebo/Documents/node_projects/cvbuilder/app.js:13:20)
Process finished with exit code 1
我的注册控制器文件
exports.displayRegister = (req, res) => {
res.render('user/register',
{
title: 'Register'
}
);
};
exports.postregister = function (req, res, next) {
body('name').isLength({min: 1}).trim().withMessage('Name empty.')
.isAlpha().withMessage('Name must be alphabet letters.')
let user = {
id: uid.time(),
fullname: req.body.fullname,
gender: req.body.gender,
dob: req.body.dob,
city: req.body.city,
country: req.body.country,
email: req.body.email,
mobile: req.body.mobile
}
let sql = "INSERT INTO user_info SET ?";
connection.query(sql, user, function(err, rows){
if(err){
//show errors on page
console.log("Error" + err)
//render register page
res.render(
'user/register',
{
message: 'successful'
}
)
}else{
console.log("Successful" + rows)
res.redirect('/');
console.log(user.uid);
}
});
}
我的路线文件
const express = require('express');
const router = express.Router();
const { body,validationResult,sanitizeBody } = require('express-validator');
const connection = require('../lib/db');
const flash = require('express-flash');
const bodyParser = require('body-parser');
const uid = require('uniqid');
const registercontroller = require('../controllers/register');
/* GET home page. */
router.get('/', function(req, res, next) {
res.render(
'user/index',
{
title: 'Home'
}
);
});
// GET register page
router.get('/register', registercontroller.displayRegister());
//Register User
router.post('/register', registercontroller.postregister());
module.exports = router;
我的app.js文件
const createError = require('http-errors');
const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const { check, validationResult } = require('express-validator');
const flash = require('express-flash');
const session = require('express-session');
const bodyParser = require('body-parser');
const mysql = require('mysql');
const connection = require('./lib/db.js');
const indexRouter = require('./routes/index');
const userRouter = require('./routes/user');
const employerRouter = require('./routes/employer');
const adminRouter = require('./routes/admin');
const app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use('/user', userRouter);
//app.use('/employer', employerRouter);
//app.use('/admin', adminRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
答案 0 :(得分:0)
您错误地注册了路由,从而调用了函数(从而传递了它们的返回值),而不是仅仅传递了函数本身。
尝试以下方法:
// GET register page
router.get('/register', registercontroller.displayRegister);
//Register User
router.post('/register', registercontroller.postregister);