Q. express-ejs-layout 적용 시 res.render 의 DB변수 is not defined 오류

기본 아바타 개발뉴비 2019. 01. 18. 조회수 513


express-ejs-layout 적용하는데 랜더에서 설정해준 mysql의 쿼리의 결과가 계속 is not defined 오류가 뜹니다. 데이터는 잘 나오는데 터미널에 계속 일부 파일의 404 오류와 함께 저 오류가 뜨네요

======================================
app.js
================================

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');
var scheduleRouter = require('./routes/schedule');

var app = express();
var expressLayouts = require('express-ejs-layouts');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(expressLayouts);
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static((__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/schedule', scheduleRouter);

// 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;

===========================================
routes/index.js
============================================

var express = require('express');
var router = express.Router();
var mysql_dbc = require('../db_config')();
var connection = mysql_dbc.init();
mysql_dbc.test_open(connection);

/* GET home page.*/
router.get('/', function(req, res, next) {
  var sql = 'SELECT * FROM user';
  connection.query(sql, function(err,result){
    res.render('layout', {user:result});
  });
});

module.exports = router;

====================
layout.ejs
=====================

<!DOCTYPE html>
<html lang="ko">
<head>
    <% include ./head.ejs %>
</head>
<body>
    <header>
        <% include ./navbar.ejs %>
        <!--navbar side-->
        <div class="container-fluid">
          <div class="row">
           <% include ./sidebar.ejs %>
    </header>
            <!--content-->
            <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
            <h3 class="page-header">
                <% include ./home.ejs %></h3>
        </main>
          </div>
        </div>
    <footer>
        푸터영역
    </footer>
    <% include ./script.ejs %> 
</body>
</html>

===================
home.ejs
====================

        <%= user[0].nic %>님의 GSP
            <!--Goal-->
            <% include ./data/goal/no-goal.ejs %>
            <% include ./data/goal/gysnpn.ejs %>

오류

[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./bin/www start`
mysql is connected successfully.
GET / 200 86.090 ms - 11304
GET /bootstrap/css/bootstrap.min.css 304 8.812 ms - -
GET /bootstrap/css/templates.css 304 16.705 ms - -
GET /bootstrap/css/home.css 304 16.099 ms - -
GET /bootstrap/docs/assets/js/ie-emulation-modes-warning.js 304 5.276 ms - -
GET /images/goal.png 304 3.077 ms - -
GET /bootstrap/js/bootstrap.min.js 304 6.311 ms - -
GET /bootstrap/docs/assets/js/docs.min.js 304 2.343 ms - -
GET /bootstrap/docs/assets/js/ie10-viewport-bug-workaround.js 304 6.762 ms - -
GET /assets/js/vendor/popper.min.js 404 70.339 ms - 2434
GET /dist/js/bootstrap.min.js 404 71.196 ms - 2434
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
GET /assets/js/ie10-viewport-bug-workaround.js 404 119.800 ms - 2434
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
GET /assets/js/vendor/popper.min.js 404 21.023 ms - 2434
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
GET /dist/js/bootstrap.min.js 404 18.424 ms - 2434
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main">
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20
GET /assets/js/ie10-viewport-bug-workaround.js 404 21.642 ms - 2434
ReferenceError: C:\Users\심현보\Desktop\gsp4\views\layout.ejs:17
    15|             <main class="col-sm-9 ml-sm-auto col-md-10 pt-3" role="main"></main>
    16|             <h3 class="page-header">
 >> 17|                 <% include ./home.ejs %></h3>
    18|         </main>
    19|           </div>
    20|         </div>

C:\Users\심현보\Desktop\gsp4\views\home.ejs:1
 >> 1|         dddddddddddddddddddd<%= user[0].nic %>님의 GSPddddddddd
    2|             <!--Goal-->
    3|             <% include ./data/goal/no-goal.ejs %>
    4|             <% include ./data/goal/gysnpn.ejs %>

user is not defined
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:56:26)
    at eval (eval at compile (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:592:12), <anonymous>:101:10)
    at returnedFn (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:623:17)
    at tryHandleCache (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:251:36)
    at View.exports.renderFile [as engine] (C:\Users\심현보\Desktop\gsp4\node_modules\ejs\lib\ejs.js:480:10)
    at View.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\application.js:592:3)
    at ServerResponse.render (C:\Users\심현보\Desktop\gsp4\node_modules\express\lib\response.js:1008:7)
    at C:\Users\심현보\Desktop\gsp4\node_modules\express-ejs-layouts\lib\express-layouts.js:113:20


공유하고 보상받기 ♥︎
댓글 0

1개의 답변이 있습니다.

큐레이터 채택
zerocho 답변자인증
Node.js 교과서 저자 2019. 01. 18 100%의 채택

express-ejs-layout을 사용하셨으면서 정작 기능은 하나도 사용하지 않으셨네요.

지금 쓰고 계신건 그냥 ejs의 include 문법입니다.

https://www.npmjs.com/package/express-ejs-layouts 보시고 코드를 수정해보세요

레이아웃을 렌더링하면 안되고 그 안의 컨텐츠를 렌더링하셔야 합니다. 컨텐츠에서 레이아웃을 불러오고요.

참고로 이런 이유때문에 저는 ejs를 권장하지 않습니다. nunjucks를 쓰시는 걸 추천드려요 Pug나요.

댓글 0