質問編集履歴

3

ソース修正

2019/08/31 05:27

投稿

Nomuty
Nomuty

スコア12

test CHANGED
File without changes
test CHANGED
@@ -28,38 +28,6 @@
28
28
 
29
29
  〇ソース
30
30
 
31
- // 基本設定
32
-
33
- var express = require('express');
34
-
35
- var router = express.Router();
36
-
37
-
38
-
39
- // MySQL設定
40
-
41
- var mysql = require('mysql');
42
-
43
- // node-configモジュールの読み込み
44
-
45
- const config = require('config');
46
-
47
- // DBの接続情報の読み込み
48
-
49
- const dbConfig = config.get('db');
50
-
51
-
52
-
53
- //bookshelf設定
54
-
55
- var knex = require('knex')({
56
-
57
- client: 'mysql',
58
-
59
- connection: dbConfig
60
-
61
- });
62
-
63
31
  var Bookshelf = require('bookshelf')(knex);
64
32
 
65
33
  Bookshelf.plugin('pagination');
@@ -67,30 +35,6 @@
67
35
  var MyData = Bookshelf.Model.extend({
68
36
 
69
37
  tableName: 'company'
70
-
71
- });
72
-
73
-
74
-
75
- // 初期アクセス時(GETアクセス)の処理
76
-
77
- router.get('/', (req, res, next) => {
78
-
79
- new MyData().orderBy('COMPANY_ID','DESC')
80
-
81
- .fetchAll().then((collection) => {
82
-
83
- var data = {
84
-
85
- title: 'DB(Mysql)からデータ取得・一覧表示(bookshelf)',
86
-
87
- content: collection.toArray()
88
-
89
- };
90
-
91
- res.render('dblistbook', data);
92
-
93
- })
94
38
 
95
39
  });
96
40
 

2

ソース修正

2019/08/31 05:27

投稿

Nomuty
Nomuty

スコア12

test CHANGED
File without changes
test CHANGED
@@ -132,32 +132,4 @@
132
132
 
133
133
 
134
134
 
135
- router.post('/post',(req, res, next) => {
136
-
137
- //1レコードのみ抽出時
138
-
139
- new MyData().where('company_name', '=', req.body['company_name']).fetchAll().then((collection) => {
140
-
141
- var data = {
142
-
143
- title: 'DB(Mysql)からデータ取得・一覧表示(bookshelf)',
144
-
145
- content: collection.toArray()
146
-
147
- //1レコードのみ抽出時
148
-
149
- //mydata: collection
150
-
151
- };
152
-
153
- console.log(req.body['company_name']);
154
-
155
- res.render('dblistbook', data);
156
-
157
- })
158
-
159
- });
160
-
161
-
162
-
163
135
  module.exports = router;

1

ソース追加

2019/08/31 05:19

投稿

Nomuty
Nomuty

スコア12

test CHANGED
File without changes
test CHANGED
@@ -23,3 +23,141 @@
23
23
  〇エラー
24
24
 
25
25
  {"error":true,"data":{"message":"select count(distinct `company`.`id`) from `company` - ER_BAD_FIELD_ERROR: Unknown column 'company.id' in 'field list'"}
26
+
27
+
28
+
29
+ 〇ソース
30
+
31
+ // 基本設定
32
+
33
+ var express = require('express');
34
+
35
+ var router = express.Router();
36
+
37
+
38
+
39
+ // MySQL設定
40
+
41
+ var mysql = require('mysql');
42
+
43
+ // node-configモジュールの読み込み
44
+
45
+ const config = require('config');
46
+
47
+ // DBの接続情報の読み込み
48
+
49
+ const dbConfig = config.get('db');
50
+
51
+
52
+
53
+ //bookshelf設定
54
+
55
+ var knex = require('knex')({
56
+
57
+ client: 'mysql',
58
+
59
+ connection: dbConfig
60
+
61
+ });
62
+
63
+ var Bookshelf = require('bookshelf')(knex);
64
+
65
+ Bookshelf.plugin('pagination');
66
+
67
+ var MyData = Bookshelf.Model.extend({
68
+
69
+ tableName: 'company'
70
+
71
+ });
72
+
73
+
74
+
75
+ // 初期アクセス時(GETアクセス)の処理
76
+
77
+ router.get('/', (req, res, next) => {
78
+
79
+ new MyData().orderBy('COMPANY_ID','DESC')
80
+
81
+ .fetchAll().then((collection) => {
82
+
83
+ var data = {
84
+
85
+ title: 'DB(Mysql)からデータ取得・一覧表示(bookshelf)',
86
+
87
+ content: collection.toArray()
88
+
89
+ };
90
+
91
+ res.render('dblistbook', data);
92
+
93
+ })
94
+
95
+ });
96
+
97
+
98
+
99
+ router.get('/:page', (req, res, next) => {
100
+
101
+ var pg = req.params.page;
102
+
103
+ pg *= 1;
104
+
105
+ if (pg < 1){ pg = 1; }
106
+
107
+ new MyData().fetchPage({page:pg, pageSize:3}).then((collection) => {
108
+
109
+ var data = {
110
+
111
+ title: 'DB(Mysql)からデータ取得・一覧表示(bookshelf)',
112
+
113
+ content: collection.toArray(),
114
+
115
+ pagination:collection.pagination
116
+
117
+ };
118
+
119
+ console.log(collection.pagination);
120
+
121
+ res.render('dblistbook', data);
122
+
123
+ })
124
+
125
+ .catch((err) => {
126
+
127
+ res.status(500).json({error: true, data: {message: err.message}});
128
+
129
+ });
130
+
131
+ });
132
+
133
+
134
+
135
+ router.post('/post',(req, res, next) => {
136
+
137
+ //1レコードのみ抽出時
138
+
139
+ new MyData().where('company_name', '=', req.body['company_name']).fetchAll().then((collection) => {
140
+
141
+ var data = {
142
+
143
+ title: 'DB(Mysql)からデータ取得・一覧表示(bookshelf)',
144
+
145
+ content: collection.toArray()
146
+
147
+ //1レコードのみ抽出時
148
+
149
+ //mydata: collection
150
+
151
+ };
152
+
153
+ console.log(req.body['company_name']);
154
+
155
+ res.render('dblistbook', data);
156
+
157
+ })
158
+
159
+ });
160
+
161
+
162
+
163
+ module.exports = router;