想在一个页面显示房间信息,于是照着一个curd的demo来写自己的项目,但是打开后出现如下错误:
ReferenceError: E:\code\kk\SecretChat-master\views\roomview.html:51
49| </thead>
50| <tbody>
>> 51| <% rooms.forEach(function( room ){ %>
52| <tr>
53| <td><%=room._id%></td>
54| <td><%= room.roomName %></td>
rooms is not defined
at eval (eval at compile (E:\code\kk\SecretChat-master\node_modules\ejs\lib\ejs.js:524:12), <anonymous>:14:8)
at returnedFn (E:\code\kk\SecretChat-master\node_modules\ejs\lib\ejs.js:555:17)
at tryHandleCache (E:\code\kk\SecretChat-master\node_modules\ejs\lib\ejs.js:203:34)
at View.exports.renderFile [as engine] (E:\code\kk\SecretChat-master\node_modules\ejs\lib\ejs.js:412:10)
at View.render (E:\code\kk\SecretChat-master\node_modules\express\lib\view.js:128:8)
at tryRender (E:\code\kk\SecretChat-master\node_modules\express\lib\application.js:640:10)
at EventEmitter.render (E:\code\kk\SecretChat-master\node_modules\express\lib\application.js:592:3)
at ServerResponse.render (E:\code\kk\SecretChat-master\node_modules\express\lib\response.js:966:7)
at E:\code\kk\SecretChat-master\routes\roomview.js:19:9
at Layer.handle [as handle_request] (E:\code\kk\SecretChat-master\node_modules\express\lib\router\layer.js:95:5)
at next (E:\code\kk\SecretChat-master\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (E:\code\kk\SecretChat-master\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (E:\code\kk\SecretChat-master\node_modules\express\lib\router\layer.js:95:5)
at E:\code\kk\SecretChat-master\node_modules\express\lib\router\index.js:281:22
at Function.process_params (E:\code\kk\SecretChat-master\node_modules\express\lib\router\index.js:335:12)
at next (E:\code\kk\SecretChat-master\node_modules\express\lib\router\index.js:275:10)
roomview.html部分代码:
<h1>房间列表</h1>
<table>
<thead>
<tr>
<th>id</th>
<th>roomName</th>
<th>roomContent</th>
<th>roomCreateUser</th>
<th>roomImg</th>
<th>roomCreateDate</th>
<th>roomJoinPeos</th>
<th>roomChat</th>
</tr>
</thead>
<tbody>
<% rooms.forEach(function( room ){ %>
<tr>
<td><%= room._id%></td>
<td><%= room.roomName %></td>
<td><%= room.roomContent %></td>
<td><%= room.roomCreateUser %></td>
<td><%= room.roomImg %></td>
<td><%= room.roomCreateDate %></td>
<td><%= room.roomJoinPeos %></td>
<td><%= room.roomImg %></td>
<td><a href="del.html?id=<%=room._id%>">删除房间</a> | <a href="modifyroom.html?id=<%=demo._id%>">更新房间信息</a></td>
</tr>
<% }); %>
</tbody>
路由roomview.js代码:
var fs = require('fs');
var formidable = require('formidable');
var util = require('util');
var express = require('express');
var multer = require('multer');
var roommodal = require('../src/db/models/room');
var user=require('../src/db/models/user');
var adminuser=require('../src/db/models/adminuser');
var upload = multer({ dest: 'uploads/' });
var result = require('../src/utils/result');
var router = express.Router();
router.get('/roomview',function(req,res,next){
if(req.session.userid){
return next();
}
res.redirect('/adminlogin');
});
router.get('/',function(req,res,next){
res.render('roomview.html',{userid:req.session.userid});
});
var Room=roommodal.Room;
exports.roomview = function(req, res,next){
Room.find(function(err,docs){
res.render('roomview', {
rooms:docs
});
});
};
module.exports = router;
模型room.js部分代码:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var roomSchema = new Schema({
roomName:String,
roomContent:String,
roomCreateDate:Date,
roomImg:String,
roomCreateUser:String,
roomJoinPeos:Number,
roomChat:String
});
var room = mongoose.model('Room',roomSchema);
module.exports = room;
server.js部分代码:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var session = require('express-session');
var loginRouter = require('./routes/login');
var logoutRouter = require('./routes/logout');
var indexRouter = require('./routes/index');
var roomviewRouter = require('./routes/roomview');
var registerRouter = require('./routes/register');
var adminloginRouter = require('./routes/adminlogin');
var adminregisterRouter = require('./routes/adminregister');
var app = express();
app.set('PORT',3000);
var server = app.listen(app.get('PORT'),function(err){
if(err){
console.error('server error:%s',err && ess.message);
return;
}
console.log('server listening at :::',app.get('PORT'));
});
app.use(session({
secret:'recommand 128 bytes random string',
cookie:{maxAge:60*1000*1000}
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extened:true}));
app.use('/public',express.static(path.join(__dirname,'/public')));
app.use('/uploads',express.static(path.join(__dirname,'/uploads')));
var ejs = require('ejs');
app.engine('html',ejs.__express);
app.set('view engine', 'html');
app.use('/login',loginRouter);
app.use('/adminlogin',adminloginRouter);
app.use('/',indexRouter);
app.use('/roomview',roomviewRouter);
app.use('/register',registerRouter);
app.use('/adminregister',adminregisterRouter);
app.get('/room/users/:roomId',function(req,res,next){
res.json(rooms[req.params.roomId]);
});
app.get('/adminlogout',function(req,res,next){
if(req.session.userid){
var user = req.session.userid;
delete req.session.userid;
}
res.redirect('/adminlogin');
});
我猜测是在roomview.js这个路由出了问题而不是server.js,然后模型room.js是不能改的,所以应该只能改roomview.js,请问各位大神,怎么改呢?
PHP中文网2017-04-17 16:31:57
server.js部分代码挂载太多app.use(),根本用不着。
路由roomview.js代码都不符合语法。 exports.roomview 是从哪里学到的?还能搭配 module.exports 使用?把 exports.roomview 改写成 router.get()模式。以下代码不符合语法。
exports.roomview = function(req, res,next){
Room.find(function(err,docs){
res.render('roomview', {
rooms:docs
});
});
};
你给出页面错误信息,也给出你打开的页面链接呀。要不我怎么知道是打开/roomview出错?还是打开/roomview/123还是打开/roomview/roomview出错?
根据错误信息,你是想打开/roomview页面,你的 路由 roomview.js 的 exports.roomview 根本就没有走到。走的是路由 roomview.js 的 route.get('/', callback),这个 callback 没有设置 rooms, 所以报错,把 exports.roomview 引入 callback。大致如下。
router.get('/', function(req,res,next){
Room.find(function(err,docs){
res.render('roomview', {
userid: req.session.userid,
rooms:docs
});
});
});
soonfy
伊谢尔伦2017-04-17 16:31:57
你在控制台 把你render 到页面的 rooms 数据打出来,看有木有,我怎么感觉 你写错了。 你在room.js里面 暴露出去了 room;然后 你在roomview.js路由方法怎么写var Room=roommodal.Room; 直接 roommodal.find 就好了,后面的 Room 是啥!!!