Node.jsで簡単なクローンアプリを作っています。エラーになってしまうため、jobs未定義の修正場所と方法を教えていただきたいです。
ReferenceError: /Users/Desktop/Node.js/Recruit/views/top.ejs:70 68| </div> 69| </li> >> 70| <% jobs.forEach((job) => { %> 71| <li class="job"><div class="job-title"><%= job.title %></div> 72| <p><img src="/images/list3.jpeg" alt="3"></p> 73| <p><%= job.point %></p> jobs is not defined at eval (eval at compile (/Users/Desktop/Node.js/Recruit/node_modules/ejs/lib/ejs.js:662:12), <anonymous>:12:8) at top (/Users/Desktop/Node.js/Recruit/node_modules/ejs/lib/ejs.js:692:17) at tryHandleCache (/Users/Desktop/Node.js/Recruit/node_modules/ejs/lib/ejs.js:272:36) at View.exports.renderFile [as engine] (/Users/Desktop/Node.js/Recruit/node_modules/ejs/lib/ejs.js:489:10) at View.render (/Users/Desktop/Node.js/Recruit/node_modules/express/lib/view.js:135:8) at tryRender (/Users/Desktop/Node.js/Recruit/node_modules/express/lib/application.js:640:10) at Function.render (/Users/Desktop/Node.js/Recruit/node_modules/express/lib/application.js:592:3) at ServerResponse.render (/Users/Desktop/Node.js/Recruit/node_modules/express/lib/response.js:1012:7) at /Users/Desktop/Node.js/Recruit/app.js:8:7 at Layer.handle [as handle_request] (/Users/Desktop/Node.js/Recruit/node_modules/express/lib/router/layer.js:95:5) ```データベースで以下のように値を入れました。
mysql> DESCRIBE jobs;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | char(70) | YES | | NULL | |
| point | varchar(50) | YES | | NULL | |
| contents | varchar(500) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
4 rows in set (0.02 sec)
mysql> INSERT INTO jobs(title, point, contents) VALUES('タイトル', 'ポイント', '募集しています');
Query OK, 1 row affected (0.02 sec)
mysql> SELECT * FROM jobs;
+----+--------------+--------------+-----------------------+
| id | title | point | contents |
+----+--------------+--------------+-----------------------+
| 1 | タイトル | ポイント | 募集しています |
+----+--------------+--------------+-----------------------+
1 row in set (0.00 sec)
mysql>
(追記2)修正済みです。回答欄で流れを確認できます。 ```js const express = require('express'); const mysql = require('mysql'); const app = express(); app.use(express.static('public')); app.use(express.urlencoded({extended: false})); const connection = mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'Recruit' }); app.get('/', (req, res) => { res.render('top.ejs'); }); app.get('/job', (req, res) => { connection.query( 'SELECT * FROM jobs', (error, results) => { res.render('job.ejs', { jobs: results }); } ); }); app.get('/content/:id', (req, res) => { const id = req.params.id; connection.query( 'SELECT * FROM jobs WHERE id = ?', [id], (error, results) => { res.render('content.ejs', { job: results[0] }); } ); }); app.listen(4090);
ejs
1<!DOCTYPE html> 2<html> 3 <head> 4 <meta charset="utf-8"> 5 <title>Recruit</title> 6 <link rel="stylesheet" href="/css/style.css"> 7 <script src="/send_url.js"></script> 8 </head> 9 <body> 10 <header> 11 <li><a href="/job">求人</a></li> 12 </header> 13 <main> 14 <ul> 15 <% jobs.forEach((job) => { %> 16 <li class="job"><div class="job-title"><%= job.title %></div> 17 <p><img src="/images/list3.jpeg" alt="3"></p> 18 <p><%= job.point %></p> 19 <button><a href="/content/<%= job.id %>">詳細を見る</a></button> 20 </li> 21 <% )} %> 22 </ul> 23 </main> 24 <%- include('footer'); %> 25 </body> 26</html> 27```よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/04/05 05:18
2021/04/05 05:24
2021/04/06 12:03 編集
2021/04/06 11:47