해결됨
[리뉴얼] Node.js 교과서 - 기본부터 프로젝트 실습까지
nunjucks res.render('error'); 작동을 안합니다.
안녕하세요. 강사님.현재 learn-sequelize MySQL 부분을 실습하고 있습니다.그런데 실습 과정중 이름(UNIQUE INDEX ) 을 중복되게 생성하여 일부러 오류를 일으켰으나error.html 페이지가 호출 되지 않고있습니다.사용자 생성 router 부분은router.route('/').get(async (req, res, next) => {try {const users = await User.findAll();res.json(users);} catch(err) {console.error(err);next(err);}}).post(async (req, res, next) => {try {const user = await User.create({name : req.body.name,age : req.body.age,married : req.body.married});console.log(user);res.status(201).json(user); // status 201 작성됨(생성됨) 뜻} catch(err) {console.error(err);next(err); // <== next(err) 로 전달하나 error.html을 호출하지 못함}});입니다.app.use((err, req, res, next) => { res.locals.message = err.message; res.locals.error = process.env.NODE_ENV !== 'production' ? err : {}; err.status = err?.status ?? 500; res.status(err.status); console.log('test'); //<== 로그는 찍힘 res.render('error');//error.html render안됨});res.render('error') 가 되지 않고 있습니다. 라우터 경로 테스트 결과app.use((req, res, next) => {const error = new Error(`${req.method} ${req.url} 라우터가 없습니다.`);error.status = 404;next(error);});라우터 경로 오류시에는 정상적으로 호출하는 것으로 보아 문제는 없는 것 같은데 원인을 찾지 못하고 있습니다.잘 부탁드립니다.-- 에러 로그는 다음과 같습니다. ---- users 테이블에 중복된 이름을 입력 하였을 때 오류 내용으로 500 에러로 보아error.html 호출이 되어야 하지만 호출이 되지 않고 있습니다.Executing (default): INSERT INTO users (`id`,`name`,`age`,`married`,`created_at`) VALUES (DEFAULT,?,?,?,?);Errorat Query.run (D:\nodeExer\node_exer1\learn-sequelize\node_modules\sequelize\lib\dialects\mysql\query.js:52:25)at D:\nodeExer\node_exer1\learn-sequelize\node_modules\sequelize\lib\sequelize.js:314:28at processTicksAndRejections (internal/process/task_queues.js:95:5)at async MySQLQueryInterface.insert (D:\nodeExer\node_exer1\learn-sequelize\node_modules\sequelize\lib\dialects\abstract\query-interface.js:308:21)at async User.save (D:\nodeExer\node_exer1\learn-sequelize\node_modules\sequelize\lib\model.js:2432:35)at async Function.create (D:\nodeExer\node_exer1\learn-sequelize\node_modules\sequelize\lib\model.js:1344:12)at async D:\nodeExer\node_exer1\learn-sequelize\routes\users.js:22:26 {name: 'SequelizeUniqueConstraintError',errors: [ValidationErrorItem {message: 'name_UNIQUE must be unique',type: 'unique violation',path: 'name_UNIQUE',value: 'aa',origin: 'DB',instance: [User],validatorKey: 'not_unique',validatorName: null,validatorArgs: []}],parent: Error: Duplicate entry 'aa' for key 'users.name_UNIQUE'at Packet.asError (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\packets\packet.js:728:17)at Execute.execute (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\commands\command.js:29:26)at Connection.handlePacket (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:456:32)at PacketParser.onPacket (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:85:12)at PacketParser.executeStart (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\packet_parser.js:75:16)at Socket.<anonymous> (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:92:25)at Socket.emit (events.js:400:28)at addChunk (internal/streams/readable.js:293:12)at readableAddChunk (internal/streams/readable.js:267:9)at Socket.Readable.push (internal/streams/readable.js:206:10) {code: 'ER_DUP_ENTRY',errno: 1062,sqlState: '23000',sqlMessage: "Duplicate entry 'aa' for key 'users.name_UNIQUE'",sql: 'INSERT INTO users (`id`,`name`,`age`,`married`,`created_at`) VALUES (DEFAULT,?,?,?,?);',parameters: [ 'aa', '11', false, '2022-12-14 17:38:07' ]},original: Error: Duplicate entry 'aa' for key 'users.name_UNIQUE'at Packet.asError (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\packets\packet.js:728:17)at Execute.execute (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\commands\command.js:29:26)at Connection.handlePacket (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:456:32)at PacketParser.onPacket (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:85:12)at PacketParser.executeStart (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\packet_parser.js:75:16)at Socket.<anonymous> (D:\nodeExer\node_exer1\learn-sequelize\node_modules\mysql2\lib\connection.js:92:25)at Socket.emit (events.js:400:28)at addChunk (internal/streams/readable.js:293:12)at readableAddChunk (internal/streams/readable.js:267:9)at Socket.Readable.push (internal/streams/readable.js:206:10) {code: 'ER_DUP_ENTRY',errno: 1062,sqlState: '23000',sqlMessage: "Duplicate entry 'aa' for key 'users.name_UNIQUE'",sql: 'INSERT INTO users (`id`,`name`,`age`,`married`,`created_at`) VALUES (DEFAULT,?,?,?,?);',parameters: [ 'aa', '11', false, '2022-12-14 17:38:07' ]},fields: { name_UNIQUE: 'aa' },sql: 'INSERT INTO users (`id`,`name`,`age`,`married`,`created_at`) VALUES (DEFAULT,?,?,?,?);'}POST /users 500 52.768 ms - 785