「作成するボタン」を押したら
item.acolumnを表示させるプログラムを書きたいのですが、
localhost:3000/newにアクセスしたときatable is not definedと表示されます。
以下参考画像
//app.js const express = require('express'); const app = express(); const mysql = require('mysql'); const connection =mysql.createConnection({ host: 'localhost', user: 'root', password: '', database: 'testdb' }); app.use(express.urlencoded({extended: false})); //ここまでは設定用コード app.get('/new', (req, res) => { res.render('new.ejs'); }); app.post("/new",(req,res)=>{ connection.query( "INSERT INTO atable(acolumn) VALUES(?)", [req.body.text], (error, results) =>{ connection.query( "SELECT acolumn FROM atable", (error, results)=>{ res.render("new.ejs",{atable: results}); } ); } ); }); app.listen(3000);
//new.ejs <form action="" method="post"> <input name="text" type="text"> <input type="submit" value="作成する"> </form> <% atable.forEach((item) => { %> <%= item.acolumn %> <% }) %>
回答1件
あなたの回答
tips
プレビュー