質問編集履歴

1

コード全体とエラーコードの追加

2019/01/31 08:12

投稿

EjiOsa
EjiOsa

スコア18

test CHANGED
File without changes
test CHANGED
@@ -67,3 +67,395 @@
67
67
  いずれはvuexも使用をするつもりですが、今は動きの確認中なのでvuexは使用せずpropsでの動作を試みています。
68
68
 
69
69
  ヒントだけでも教えていただけると助かります。
70
+
71
+
72
+
73
+ #追記
74
+
75
+ 実際に動かしているコンポーネントの全体像になります。
76
+
77
+ 以下のハードコード時はvueDevツール内のpropsとcomputedには文字列が入っております。
78
+
79
+
80
+
81
+ ```vue
82
+
83
+ <template>
84
+
85
+ <div>
86
+
87
+ <div class="head">
88
+
89
+ <h2>User Registration</h2>
90
+
91
+ </div>
92
+
93
+ <ul class="side-bar-list">
94
+
95
+ <SideBar
96
+
97
+ v-for="user in users"
98
+
99
+ :key="user.id"
100
+
101
+ :userData="user"
102
+
103
+ @userEdit="userEdit"
104
+
105
+ ></SideBar>
106
+
107
+ </ul>
108
+
109
+ </div>
110
+
111
+ </template>
112
+
113
+
114
+
115
+ <script>
116
+
117
+ import SideBar from './registration_components/SideBar';
118
+
119
+
120
+
121
+ const ApiUrl = 'http://localhost/*******/*******/public/api/items';
122
+
123
+ //====================================================================
124
+
125
+ //ここの部分に問題があると考えています。
126
+
127
+
128
+
129
+ var headers = {
130
+
131
+ 'Accept': 'application/json',
132
+
133
+ // 'Authorization': 'Bearer '+'uGI09ICALLdclOBe8UtTvykugiydnoJkcc6pHMmRCi4KhALJShbN2s8KmTdi',//このようにハードコードするとエラーがありません。
134
+
135
+ 'Authorization': 'Bearer '.concat(this.tokenNo),
136
+
137
+ };
138
+
139
+
140
+
141
+ //====================================================================
142
+
143
+ let axios = require('axios').create({
144
+
145
+ baseURL: ApiUrl,
146
+
147
+ headers: headers,
148
+
149
+ responseType: 'json'
150
+
151
+ });
152
+
153
+
154
+
155
+ export default {
156
+
157
+ name: 'UserControl',
158
+
159
+ props:{
160
+
161
+ tokenData: {
162
+
163
+ type: String,
164
+
165
+ // required: true
166
+
167
+ }
168
+
169
+ },
170
+
171
+ components: {
172
+
173
+ SideBar
174
+
175
+ },
176
+
177
+ computed:{
178
+
179
+ tokenNo() {
180
+
181
+ return this.tokenData
182
+
183
+ }
184
+
185
+ },
186
+
187
+ data () {
188
+
189
+ return {
190
+
191
+ users: null,
192
+
193
+ editUser: {
194
+
195
+ first_name: '',
196
+
197
+ family_name: '',
198
+
199
+ age: '',
200
+
201
+ email: '',
202
+
203
+ password: '',
204
+
205
+ edit: false
206
+
207
+ }
208
+
209
+ };
210
+
211
+ },
212
+
213
+ mounted () {
214
+
215
+ axios.get(ApiUrl)
216
+
217
+ .then(response => (this.users = response.data));
218
+
219
+ },
220
+
221
+ methods: {
222
+
223
+ // edit method
224
+
225
+ clearEdit () {
226
+
227
+ this.editUser = {
228
+
229
+ first_name: '',
230
+
231
+ family_name: '',
232
+
233
+ age:'',
234
+
235
+ email: '',
236
+
237
+ password: '',
238
+
239
+ edit: false
240
+
241
+ };
242
+
243
+ },
244
+
245
+ // SideBar method
246
+
247
+ userEdit (selectUserID) { // ユーザー編集のONとOFF
248
+
249
+ let selectEditUser = this.users.findIndex(({ id }) => id === selectUserID);// IDから編集を選択されたユーザー取得
250
+
251
+ this.releaseEdit(selectEditUser);
252
+
253
+ this.users[selectEditUser].edit = !this.users[selectEditUser].edit;
254
+
255
+ this.setCurrentUser();
256
+
257
+ this.$forceUpdate();
258
+
259
+ },
260
+
261
+ releaseEdit (userIndex) { // 選択したユーザー以外が編集中だった場合に解除
262
+
263
+ let editTrueIndex = this.users.findIndex(({ edit }) => edit === true);// 編集中のユーザーインデックスを取得
264
+
265
+ if (editTrueIndex > -1) { // indexで取得のためindexが有効かチェック
266
+
267
+ if (this.users[editTrueIndex].edit !== this.users[userIndex].edit) { // 編集中のユーザーと選択したユーザーが違う場合に削除発火
268
+
269
+ this.users[editTrueIndex].edit = false;
270
+
271
+ }
272
+
273
+ }
274
+
275
+ },
276
+
277
+ setCurrentUser () { // 編集ONになった際に右の編集画面に反映させるメソッド
278
+
279
+ let editTrueIndex = this.users.findIndex(({ edit }) => edit === true);
280
+
281
+ if (editTrueIndex > -1) {
282
+
283
+ this.editUser = this.users[editTrueIndex];
284
+
285
+ } else {
286
+
287
+ this.refresh();//編集を更新しない場合はデータベースを再取得
288
+
289
+ this.clearEdit();//右側の記入欄をクリア
290
+
291
+ }
292
+
293
+ },
294
+
295
+ //Api method
296
+
297
+ async update (data) {//ユーザー情報の更新
298
+
299
+ await axios.put(ApiUrl+'/'+data.id, data)
300
+
301
+ .then(
302
+
303
+ alert('user edit ok')
304
+
305
+ )
306
+
307
+ .catch(function (err) {
308
+
309
+ alert(err)
310
+
311
+ });
312
+
313
+ this.refresh()
314
+
315
+ },
316
+
317
+ async addUser () {//Userの追加
318
+
319
+ await axios.post(ApiUrl, this.editUser)
320
+
321
+ .then(
322
+
323
+ alert('user add ok')
324
+
325
+ )
326
+
327
+ .catch(function (err) {
328
+
329
+ alert(err);
330
+
331
+ });
332
+
333
+ this.refresh()//thenの中に書いて不安定になってたけど、ここにして安定した。
334
+
335
+ },
336
+
337
+ async softDelete (id) {//Userの論理削除、Deleted_atにTimeStamp入力
338
+
339
+ await axios.delete(ApiUrl+'/'+id)
340
+
341
+ .then(
342
+
343
+ alert('user softDeleted ok')
344
+
345
+ )
346
+
347
+ .catch(function (err) {
348
+
349
+ alert(err)
350
+
351
+ });
352
+
353
+ this.refresh()
354
+
355
+ },
356
+
357
+ refresh (){//Api処理後の再表示
358
+
359
+ axios.get(ApiUrl)
360
+
361
+ .then(response => (
362
+
363
+ this.users = response.data
364
+
365
+ ))
366
+
367
+ .catch(err => (
368
+
369
+ alert(err)
370
+
371
+ ));
372
+
373
+ },
374
+
375
+ // Registration method
376
+
377
+ add () {
378
+
379
+ if (this.users.find(editId => editId.id === this.editUser.id)) { //users内にeditUserのidが存在する=>更新の場合
380
+
381
+ if (confirm('ユーザー情報を更新しますか?')) {
382
+
383
+ //↑ var result = confirm('ユーザー情報を更新しますか?')をそのまま代入している。
384
+
385
+ var editTrueIndex = this.users.findIndex(({edit}) => edit === true);// 編集中のインデックス取得
386
+
387
+ this.users[editTrueIndex].edit = !this.users[editTrueIndex].edit; // 編集ボタンを解除
388
+
389
+ this.update(this.users[editTrueIndex]);
390
+
391
+ this.clearEdit();
392
+
393
+ }
394
+
395
+ } else { // users内にeditUserのidが存在しない=>追加の場合
396
+
397
+ if (confirm('ユーザーを追加しますか?')) {
398
+
399
+ this.addUser();
400
+
401
+ this.clearEdit();
402
+
403
+ }
404
+
405
+ }
406
+
407
+ },
408
+
409
+ del () {
410
+
411
+ if (confirm('ユーザーを削除しますか?')) {//user削除
412
+
413
+ var editTrueIndex = this.users.findIndex(({edit}) => edit === true);
414
+
415
+ this.softDelete(this.users[editTrueIndex].id);
416
+
417
+ this.clearEdit();
418
+
419
+ this.refresh();
420
+
421
+ }
422
+
423
+ },
424
+
425
+ }
426
+
427
+ };
428
+
429
+ </script>
430
+
431
+
432
+
433
+ <style scoped lang="scss">
434
+
435
+
436
+
437
+ </style>
438
+
439
+ ```
440
+
441
+ methods内でもaxiosは使用していますが、動的な部分ですので初期表示のmountedがメインのエラー個所と考えています。
442
+
443
+ #エラーコード
444
+
445
+ ```vue
446
+
447
+ 'Authorization': 'Bearer '.concat(this.tokenNo)
448
+
449
+ ```
450
+
451
+ 上記、使用時のエラーコードは以下になります。
452
+
453
+ ```
454
+
455
+ app.js:2947 Uncaught TypeError: Cannot read property 'tokenNo' of undefined
456
+
457
+ ```
458
+
459
+ 恥ずかしい話、今回の質問後にエラーを確認しましたがundefinedでした。
460
+
461
+ しかし、なぜundefinedになってしまっているのかわかりません。