错误信息是:events.js:160 throw er; // Unhandled ‘error’ event ^
TypeError: Cannot set property ‘error’ of undefined at E:\jesus\routes\index.js:24:31 at Query.<anonymous> (E:\jesus\node_modules\mongoose\lib\model.js:3433:16) at E:\jesus\node_modules\kareem\index.js:273:21 at E:\jesus\node_modules\kareem\index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9)
package.json文件代码: { “name”: “jesus”, “version”: “0.0.0”, “private”: true, “scripts”: { “start”: “node ./bin/www” }, “dependencies”: { “body-parser”: “~1.16.0”, “cookie-parser”: “~1.4.3”, “debug”: “~2.6.0”, “ejs”: “~2.5.5”, “express”: “~4.14.1”, “express-session”: “^1.15.1”, “mongoose”: “^4.8.5”, “morgan”: “~1.7.0”, “multer”: “^1.3.0”, “serve-favicon”: “~2.3.2” } }
index.js文件代码: var express = require(‘express’); var router = express.Router(); var multer = require(‘multer’); var upload = multer();
/* GET index page. */ router.get(’/’, function (req, res, next) { res.render(‘index’, {title: ‘Express’}); //渲染index文件,传出title值给index.html使用 });
/GET login page/ router.route(’/login’).get(function (req, res) { res.render(“login”, {title: “登录”}); }).post(upload.single(), function (req, res) { //get User info //这里的user是从model中获取user对象,通过global.dbHandle的全局方法; var User = global.dbHandle.getModel(‘user’); var uname = req.body.uname; //获取post上来的data数据中uname的值; User.findOne({name: uname}, function (err, doc) { if (err) { res.send(500); console.log(err); } else if (!doc) { req.session.error = ‘用户名不存在’; res.send(404); // res.redirect("/login"); } else { if (req.body.upwd != doc.password) { req.session.error = “密码错误”; res.send(404); // res.redirect("/login"); } else { req.session.user = doc; res.send(200); // res.redirect("/home"); }
}
});
});
/GET register page/ router.route("/register").get(function (req, res) { res.render(“register”, {title: ‘USER register’}); }).post(upload.single(), function (req, res) { //这里的User就是Model中获取的user对象,通过global.dbHandle全局方法 var User = global.dbHandle.getModel(‘user’); var uname = req.body.uname; var upwd = req.body.upwd; User.findOne({name: uname}, function (err, doc) { //与处理/login路径相似; if (err) { res.send(500); req.session.error = ‘网络异常’; console.log(err); } else if (doc) { req.session.error = ‘用户名已经存在’; res.send(500); } else { User.create({ name: uname, password: upwd }, function (err, doc) { if (err) { res.send(500); console.log(err); } else { req.session.error = “用户名创建成功”; res.send(200); } }); } }) });
/Get home page/ router.get(’/home’, function (req, res) { if (!req.session.user) { req.session.error = “请先登录” // res.redirect(’/login’); } res.render(“home”, {title: ‘主页’}); });
/GET logout page/ router.get("/logout", function (req, res) { req.session.user = null; req.session.error =null; res.redirect(’/’); });
module.exports = router;
app.js代码:
var express = require(‘express’); var path = require(‘path’); var favicon = require(‘serve-favicon’); var logger = require(‘morgan’); var cookieParser = require(‘cookie-parser’);
var bodyParser = require(‘body-parser’); var mongoose =require(‘mongoose’); var session = require(‘express-session’);
global.dbHandle =require(’./database/dbHandle’); global.db = mongoose.connect(“mongodb://localhost:27017/nodedb”);
var index = require(’./routes/index’); var users = require(’./routes/users’);
var app = express();
// view engine setup app.set(‘views’, path.join(__dirname, ‘views’)); app.engine(“html”,require(“ejs”).__express); //这句是新增的,相当于让EJS模板识别后缀为html文件的方法。 app.set(‘view engine’, ‘html’);
// uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, ‘public’, ‘favicon.ico’))); app.use(logger(‘dev’)); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: false })); app.use(cookieParser());
app.use(express.static(path.join(__dirname, ‘public’)));
app.use(’/’, index); app.use(’/users’, users); app.use(’/register’,index); app.use(’/login’,index); app.use(’/home’,index); app.use(’/logout’,index);
app.use(function (req,res,next) { res.locals.user=req.session.user;//从session获取user对象 var err = req.session.error; //获取错误信息 delete req.session.error; res.locals.message="";//展示信息的message if(err){ res.locals.message=’<div class=“alert alert-danger” style=“margin-bottom: 20px; color: red;”>’+err+’</div>’ } next(); //中间件传递; });
//新增加multer和mongoose; app.use(bodyParser.urlencoded({extended:true})); /app.use(multer());/ //不知道这个有什么用 app.use(cookieParser());
//使用express-session模块 app.use(session({ secret:‘secret’, cookie:{ maxAge:10006030 } }));
// catch 404 and forward to error handler app.use(function(req, res, next) { var err = new Error(‘Not Found’); err.status = 404; next(err); });
// 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;
dbHandle.js代码:
var mongoose = require(‘mongoose’); var Schema = mongoose.Schema; var models = require(’./model’);
for(var m in models){ mongoose.model(m,new Schema(models[m])); }
module.exports = { getModel:function (type) { return _getModel(type); } };
var _getModel=function(type) { return mongoose.model(type); };
先提前谢谢每个帮助解答问题的高手们!!