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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Node.js

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

Express

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

Q&A

解決済

1回答

1574閲覧

【Node+Express】Cannot read property 'findAll' of undefinedと表示される

tsu2626

総合スコア12

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Node.js

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

Express

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

0グッド

0クリップ

投稿2018/10/15 15:39

編集2018/10/15 15:45

前提・実現したいこと

エラーを解決したい。

発生している問題・エラーメッセージ

デバッグ後に
http://localhost:3000/usersにアクセスをすると表示される

Cannot read property 'findAll' of undefined

該当のソースコード

/routes/user.js

js

1var models = require('../models'); //モデルの読み込み 2 3var express = require('express'); 4var router = express.Router(); 5 6/* GET users listing. */ 7router.get('/', (req, res, next) => { 8 models.User.findAll({ 9 order: [ 10 ['id', 'desc'] 11 ], 12 limit: 10 13 }).then(function(users) { 14 res.render('users', { 15 title: 'Node.js/Express入門: CRUD サンプル', 16 users: users 17 }); 18 }); 19}); 20 21router.post('/create', (req, res) => { 22 models.User.create({ 23 first_name: req.body.first, 24 last_name: req.body.last_name, 25 email: req.body.email 26 }).then( () => { 27 res.reject('/users'); 28 }) 29}) 30 31module.exports = router; 32

/models/index.js

js

1'use strict'; 2 3const fs = require('fs'); 4const path = require('path'); 5const Sequelize = require('sequelize'); 6const basename = path.basename(__filename); 7const env = process.env.NODE_ENV || 'development'; 8const config = require(__dirname + '/../config/config.json')[env]; 9const db = {}; 10 11let sequelize; 12if (config.use_env_variable) { 13 sequelize = new Sequelize(process.env[config.use_env_variable], config); 14} else { 15 sequelize = new Sequelize(config.database, config.username, config.password, config); 16} 17 18fs 19 .readdirSync(__dirname) 20 .filter(file => { 21 return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js'); 22 }) 23 .forEach(file => { 24 const model = sequelize['import'](path.join(__dirname, file)); 25 db[model.name] = model; 26 }); 27 28Object.keys(db).forEach(modelName => { 29 if (db[modelName].associate) { 30 db[modelName].associate(db); 31 } 32}); 33 34db.sequelize = sequelize; 35db.Sequelize = Sequelize; 36 37module.exports = db; 38

/routes/index.js

js

1var express = require('express'); 2var router = express.Router(); 3 4/* GET home page. */ 5router.get('/', function(req, res, next) { 6 res.render('index', { title: 'Express' }); 7}); 8 9module.exports = router; 10

試したこと

npm install -g sequelize npm install -g sequelize-cli sequelize init

このサイトを参考にしています
https://shell-mag.com/nodejs-55/

以上、よろしくおねがいします。

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

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

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

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

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

guest

回答1

0

自己解決

そもそもモデルを作っていませんでした。
Usersが無いのも当たり前です。

投稿2018/10/15 16:43

tsu2626

総合スコア12

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問