###expressでデータが取得できません。
Executing (default): SELECT "user_id", "email", "user_name", "password", "created_at" FROM "users" AS "users" WHERE "users"."user_id" = 1 ORDER BY "users"."created_at" DESC;
Unhandled rejection ReferenceError: user_id is not defined
at /home/vagrant/w/s/routes/index.js:17:11
at tryCatcher (/home/vagrant/w/s/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/vagrant/w/s/node_modules/bluebird/js/release/promise.js:547:31)
at Promise._settlePromise (/home/vagrant/w/s/node_modules/bluebird/js/release/promise.js:604:18)
at Promise._settlePromise0 (/home/vagrant/w/s/node_modules/bluebird/js/release/promise.js:649:10)
at Promise._settlePromises (/home/vagrant/w/s/node_modules/bluebird/js/release/promise.js:729:18)
at _drainQueueStep (/home/vagrant/w/s/node_modules/bluebird/js/release/async.js:93:12)
at _drainQueue (/home/vagrant/w/s/node_modules/bluebird/js/release/async.js:86:9)
at Async._drainQueues (/home/vagrant/w/s/node_modules/bluebird/js/release/async.js:102:5)
at Immediate.Async.drainQueues [as _onImmediate] (/home/vagrant/w/s/node_modules/bluebird/js/release/async.js:15:14)
at processImmediate (internal/timers.js:439:21)
というエラーが出てしまい
dbからデータが取れません。
modelは定義しています。
dbとappの接続もできています。
user_id1のユーザーはDB上にあります。
調べてみたのですがnode.jsの方のerorrばかり出てしまい、sequelizeを使った場合の記事が確認できなかっため質問しました。
コード
route/index.js
node
1'use strict'; 2const express = require('express'); 3const router = express.Router(); 4const User = require('../models/user'); 5 6/* GET home page. */ 7router.get('/', function(req, res, next) { 8 res.render('index', { title: 'Express' }); 9 10 //var userdate = user.findAll({where:{user_id : 1},order: [['"created_at"', 'DESC']]}); 11 12 User.findAll({ 13 where:{user_id : 1}, 14 order: [['"created_at"', 'DESC']] 15 }).then((user) => { 16 res.render('user', { 17 id :user_id, 18 user_name : user_name, 19 }); 20 }); 21 22}); 23 24module.exports = router;
model/user.js
node
1'use strict'; 2const loader = require('./sequelize-loader'); 3const Sequelize = loader.Sequelize; 4 5const User = loader.database.define('users', { 6 user_id: { 7 type: Sequelize.INTEGER, 8 primaryKey: true, 9 allowNull: false, 10 autoIncrement: true 11 },email: { 12 type: Sequelize.STRING, 13 allowNull: false 14 },user_name: { 15 type: Sequelize.STRING, 16 allowNull: false 17 },password: { 18 type: Sequelize.STRING, 19 allowNull: false 20 },created_at: { 21 type: Sequelize.DATE, 22 allowNull: false 23 } 24 25},{ 26 freezeTableName: true, 27 timestamps: false 28 }); 29 30module.exports = User;
view
node
1extends layout 2 3block content 4 h1= title 5 p Welcome to #{title} 6 7 p #{id}
app.js
node
1var createError = require('http-errors'); 2var express = require('express'); 3var path = require('path'); 4var cookieParser = require('cookie-parser'); 5var logger = require('morgan'); 6var session = require('express-session'); 7var helmet = require('helmet'); 8var app = express(); 9 10var indexRouter = require('./routes/index'); 11var usersRouter = require('./routes/user'); 12var loginRouter = require('./routes/login'); 13var logoutRouter = require('./routes/logout'); 14 15 16// view engine setup 17app.set('views', path.join(__dirname, 'views')); 18app.set('view engine', 'pug'); 19 20app.use(logger('dev')); 21app.use(express.json()); 22app.use(express.urlencoded({ extended: false })); 23app.use(cookieParser()); 24app.use(express.static(path.join(__dirname, 'public'))); 25app.use(helmet()); 26 27app.use('/', indexRouter); 28app.use('/newuser', usersRouter); 29app.use('/login',loginRouter); 30app.use('/logout',logoutRouter); 31 32 33// catch 404 and forward to error handler 34app.use(function(req, res, next) { 35 next(createError(404)); 36}); 37 38// error handler 39app.use(function(err, req, res, next) { 40 // set locals, only providing error in development 41 res.locals.message = err.message; 42 res.locals.error = req.app.get('env') === 'development' ? err : {}; 43 44 // render the error page 45 res.status(err.status || 500); 46 res.render('error'); 47}); 48 49module.exports = app; 50 51 52//model 53var User = require('./models/user'); 54User.sync().then(() => { 55 56}); 57
環境
node.js 8.16.0
express 4.16.0
postgresql 10.9
sequelize 4.33.4
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。