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

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

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

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Node.js

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

Express

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

Q&A

解決済

1回答

726閲覧

expressでカラムが認識されない

nulll

総合スコア14

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Node.js

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

Express

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

0グッド

0クリップ

投稿2019/07/07 08:02

expressでカラムが認識されない

expressでがカラムであるcommentIdが認識されません
なぜ認識されないのでしょうか?
おそらく正しくdbは作成されています

エラー文

commentId is not defined ReferenceError: commentId is not defined at /home/ubuntu/environment/smp/ex/routes/index.js:16:16 at Layer.handle [as handle_request] (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/layer.js:95:5) at next (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/layer.js:95:5) at /home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:335:12) at next (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:275:10) at Function.handle (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:174:3) at router (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:47:12) at Layer.handle [as handle_request] (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:317:13) at /home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:335:12) at next (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:275:10) at serveStatic (/home/ubuntu/environment/smp/ex/node_modules/serve-static/index.js:75:16) at Layer.handle [as handle_request] (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:317:13) at /home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:335:12) at next (/home/ubuntu/environment/smp/ex/node_modules/express/lib/router/index.js:275:10) at cookieParser (/home/ubuntu/environment/smp/ex/node_modules/cookie-parser/index.js:71:5)

サーバー起動時のターミナルの出力

Executing (default): CREATE TABLE IF NOT EXISTS "comments" ("commentId" INTEGER NOT NULL , "comment" INTEGER NOT NULL, PRIMARY KEY ("commentId")); Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'comments' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;

モデルファイル

js

1'use strict'; 2const loader = require('./sequelize-loader'); 3const Sequelize = loader.Sequelize; 4 5const Comment = loader.database.define('comments', { 6 commentId: { 7 type: Sequelize.INTEGER, 8 primaryKey: true, 9 allowNull: false 10 }, 11 comment: { 12 type: Sequelize.INTEGER, 13 allowNull: false 14 }}, 15 { 16 freezeTableName: true, 17 timestamps: false 18 }); 19 20module.exports = Comment;

route/index.js

js

1var express = require('express'); 2var router = express.Router(); 3const Comment = require('../models/comment'); 4 5 6/* GET home page. */ 7router.get('/', function(req, res, next) { 8 res.render('index', { title: 'Express' }); 9}); 10 11router.post('/', function(req, res, next) { 12 13 const updatedAt = new Date(); 14 15 Comment.create({ 16 commentId: commentId, 17 comment:req.body.comment, 18 updatedAt: updatedAt 19 20 }); 21 22}); 23module.exports = router;

index.pug

js

1doctype html 2html(lang="ja") 3 head 4 meta(charset="UTF-8") 5 title comment 6 body 7 h2 新規投稿 8 form(method="post" action="/") 9 div 10 textarea(name="comment" cols=40 rows=4) 11 div 12 button(type="submit") 投稿

環境
aws cloud9
os ubuntu
node.js v8.16.0
express 4.16.0
npm 6.4.1

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

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

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

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

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

guest

回答1

0

ベストアンサー

commentIdがindex.js内に宣言されていないので、登録処理でエラーとなっています。

commentIdをユニークキーに使用するのなら、autoIncrement: trueの指定も必要だと思います。
autoIncrement: trueを指定したカラムは、値を渡さなくても自動で採番してくれるので、登録処理のcommentIdの設定は不要となります。

Comment.create({ comment:req.body.comment, updatedAt: updatedAt });

投稿2019/07/07 10:12

編集2019/07/07 10:16
Kento75

総合スコア65

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

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

nulll

2019/07/08 02:41

原因を教えていただきありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問