質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
npm

npmは、Node Packaged Modulesの略。Node.jsのライブラリ・パッケージを管理できるツールです。様々なモジュールを簡単にインストールでき、自分でモジュールを作成し公開する際にも使用できます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

Q&A

解決済

1回答

1025閲覧

【node.js/Express】npm db-migrateをgit revertで打ち消したが元の状態に戻せない(DBにPOST出来ない)

tonkotsu_ramen

総合スコア6

npm

npmは、Node Packaged Modulesの略。Node.jsのライブラリ・パッケージを管理できるツールです。様々なモジュールを簡単にインストールでき、自分でモジュールを作成し公開する際にも使用できます。

Node.js

Node.jsとはGoogleのV8 JavaScriptエンジンを使用しているサーバーサイドのイベント駆動型プログラムです。

Express

ExpressはNode.jsのWebアプリケーションフレームワークです。 マルチページを構築するための機能セットおよびハイブリッドのWebアプリケーションを提供します。

0グッド

0クリップ

投稿2021/07/07 13:18

【これまでの状況】
node.js/express.jsの学習を開始するべく、progateのnode.jsコースにてログイン、ログアウト機能を持ったTODOアプリを作成しました。
ここまで問題なく動作しており、次にDBのカラムを色々と操作したくなり、npmのdb-migrateを導入しました。ここも導入自体は出来たのですが、使用方法がよくわからなかったので、(この際、何度かマイグレートしてDBをいじってしまった。もともとあったlists,usersテーブルは一度削除し、手動で作成しなおしています。)一旦もとの状態に戻そうと思い、db-migrateを導入したコミットのみをgit revertしました。

【現在起きている問題点】
当然かもしれませんが、git revertしてもDBは元の状態に戻せていません。パスワードがハッシュ化されておらず、平文のままの表示となっております。

イメージ説明

◎できないこと
・ログインできない。
・ユーザー新規登録できない。
・TODOの新規投稿できない。

◎できること
・TODO削除できる。
・TODO変更できる。

【試してみたこと】
・npm bcryptをインストールし直してみましたが、状態は変わらずでした。
・TODOの新規投稿に関しては、ログインしていないと投稿出来ない仕様にしていましたが、その機能をコメントアウトしてみましたが、TODOの新規投稿はできませんでした。
・パスワードのハッシュ化以外は、テーブル名、カラム名は元の状態にした。
→が、状況変わらずです。

【実現したいこと、知りたいこと】

・パスワードを元どおりハッシュ化して、ログインできるようにしたい。
→ログインできないハッシュ化していないだけではないかと。同じ名前のテーブルを作成し直してもダメなのでしょうか。

・ログイン後、TODOを新規投稿できるようにしたい。

素人質問で申し訳ないですが、何卒宜しくお願い致します。

【コード】

js

1const express = require('express'); 2const mysql = require('mysql2'); 3const session = require('express-session'); 4const bcrypt = require('bcrypt'); 5const app = express(); 6 7app.use(express.static('public')); 8app.use(express.urlencoded({ extended: false })); 9 10 11const connection = mysql.createConnection({ 12 host: 'localhost', 13 user: 'root', 14 password: 'password', 15 database: 'nodejs' 16}); 17connection.connect((err) => { 18 if (err) { 19 console.log('error connecting: ' + err.stack); 20 return; 21 } 22 console.log('接続成功'); 23}); 24 25app.use( 26 session({ 27 secret: 'my_secret_key', 28 resave: false, 29 saveUninitialized: false, 30 }) 31); 32 33app.use((req, res, next) => { 34 if (req.session.userId === undefined) { 35 res.locals.username = 'ゲスト'; 36 res.locals.isLoggedIn = false; 37 console.log("ログインしていないので実行しません") 38 } else { 39 res.locals.username = req.session.username; 40 res.locals.isLoggedIn = true; 41 console.log("ログイン中") 42 } 43 next(); 44}); 45 46app.get('/', (req, res) => { 47 res.render('top.ejs'); 48}); 49app.get('/login', (req, res) => { 50 res.render('login.ejs'); 51}); 52 53app.post('/login', (req, res) => { 54 const email = req.body.email; 55 connection.query( 56 'SELECT * FROM users WHERE email = ?', [email], 57 (error, results) => { 58 if (results.length > 0) { 59 const plain = req.body.password; 60 const hash = results[0].password; 61 bcrypt.compare(plain, hash, (error, isEqual) => { 62 if (isEqual) { 63 req.session.userId = results[0].id; 64 req.session.username = results[0].name; 65 res.redirect('/index'); 66 console.log("ログインできました。") 67 } else { 68 res.redirect("/login"); 69 console.log("ログインできませんでした。") 70 } 71 }); 72 } else { 73 res.redirect('/login'); 74 } 75 }); 76}); 77 78app.get("/logout", (req, res) => { 79 req.session.destroy((error) => { 80 res.redirect("/"); 81 }); 82}); 83 84app.get('/index', (req, res) => { 85 connection.query( 86 'SELECT * FROM lists', 87 (error, results) => { 88 res.render('index.ejs', { items: results }); 89 } 90 ); 91}); 92 93app.get('/new', (req, res) => { 94 res.render('new.ejs'); 95}); 96 97app.get('/signup', (req, res) => { 98 res.render('signup.ejs', { errors: [] }); 99}); 100 101app.post('/signup', 102 (req, res, next) => { 103 console.log("入力値の空チェック") 104 const username = req.body.name; 105 const email = req.body.email; 106 const password = req.body.password; 107 const errors = []; 108 109 if (username === "") { 110 errors.push("ユーザー名がカラです。"); 111 } 112 if (email === "") { 113 errors.push("メールアドレスがカラです。"); 114 } 115 if (password === "") { 116 errors.push("パスワードがカラです。"); 117 } 118 console.log(errors); 119 if (errors.length > 0) { 120 res.render("signup.ejs", { errors: errors }); 121 } else { 122 next(); 123 } 124 }, 125 126 (req, res, next) => { 127 console.log("メアド重複チェック") 128 const email = req.body.email; 129 const errors = []; 130 connection.query( 131 'SELECT * FROM users WHERE email = ?', [email], 132 (error, results) => { 133 if (results.length > 0) { 134 errors.push("このメールアドレスは既に使われています。") 135 res.render("signup.ejs", { errors: errors }); 136 } else { 137 next(); 138 } 139 } 140 ); 141 }, 142 143 (req, res) => { 144 console.log("ユーザー登録"); 145 const username = req.body.name; 146 const email = req.body.email; 147 const password = req.body.password; 148 bcrypt.hash(password, 10, (error, hash) => { 149 connection.query( 150 'INSERT INTO users (name, email, password) VALUES (?, ?, ?)', [username, email, hash], 151 (error, results) => { 152 console.log(results) 153 req.session.userId = results.insertId; 154 req.session.username = username; 155 console.log(req.session.userId); 156 console.log(req.session.username); 157 res.redirect('/index'); 158 } 159 ); 160 }); 161 } 162); 163 164 165app.post('/create', (req, res) => { 166 console.log(req.body.listName); 167 console.log("投稿するよ"); 168 connection.query( 169 'INSERT INTO lists (name) VALUES(?)', [req.body.listName], 170 (error, results) => { 171 res.redirect('/index'); 172 }); 173}); 174 175app.post('/delete/:id', (req, res) => { 176 connection.query( 177 'DELETE FROM lists WHERE id = ?', [req.params.id], 178 (error, results) => { 179 res.redirect('/index'); 180 }); 181}); 182 183app.get('/edit/:id', (req, res) => { 184 connection.query( 185 'SELECT * FROM lists WHERE id = ?', [req.params.id], 186 (error, results) => { 187 res.render('edit.ejs', { item: results[0] }); 188 }); 189}); 190 191app.post('/update/:id', (req, res) => { 192 connection.query( 193 'UPDATE lists SET name = ? WHERE id = ?', [req.body.listName, req.params.id], 194 (error, results) => { 195 res.redirect('/index'); 196 }); 197}); 198 199 200 201app.listen(3000);

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

idカラムにauto_incrementを設定していないためでした。

投稿2021/07/07 22:05

tonkotsu_ramen

総合スコア6

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問