搜尋

首頁  >  問答  >  主體

javascript - nodejs在一个页面下显示mongodb数据库里的内容,页面出现is not defined错误,是哪里出了问题?

想在一个页面显示房间信息,于是照着一个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,请问各位大神,怎么改呢?

阿神阿神2777 天前1041

全部回覆(2)我來回復

  • PHP中文网

    PHP中文网2017-04-17 16:31:57

    問題

    1. server.js部分程式碼掛載太多app.use(),根本用不著。

    2. 路由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
              });
          });
      };
    3. 你給頁面錯誤訊息,也給你打開的頁面連結呀。要不怎麼知道是開啟/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

    回覆
    0
  • 伊谢尔伦

    伊谢尔伦2017-04-17 16:31:57

    你在控制台 把你render 到頁面的 rooms 數據打出來,看有木有,我怎麼感覺 你寫錯了。 你在room.js裡面 露出了 room;然後 你怎麼寫在roomview.js路由方法var Room=roommodal.Room; 直接 roommodal.find 就好了,後面的 Room 是啥! ! !

    回覆
    0
  • 取消回覆