質問編集履歴
2
promiseにresolveを追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
ご指摘頂いた内容にて、
|
27
|
+
~~ご指摘頂いた内容にて、
|
28
28
|
|
29
29
|
1,openIndexeddb関数にpromiseを追加
|
30
30
|
|
@@ -38,7 +38,13 @@
|
|
38
38
|
|
39
39
|
どこを修正すれば、良いのでしょうか?
|
40
40
|
|
41
|
-
|
41
|
+
~~
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
ありがとうございます。
|
46
|
+
|
47
|
+
再度ご指摘頂いた内容で修正し、動作はしたようですが、知識不足な点が多いため、勉強します。
|
42
48
|
|
43
49
|
```
|
44
50
|
|
@@ -94,226 +100,236 @@
|
|
94
100
|
|
95
101
|
|
96
102
|
|
97
|
-
function openIndexeddb(){
|
103
|
+
function openIndexeddb(){
|
98
|
-
|
104
|
+
|
99
|
-
return new Promise(
|
105
|
+
return new Promise((resolve,reject) =>{
|
100
|
-
|
106
|
+
|
101
|
-
alert("1データベースを開く");
|
107
|
+
alert("1データベースを開く");
|
102
|
-
|
108
|
+
|
103
|
-
if (indexedDB) {
|
109
|
+
if (indexedDB) {
|
104
|
-
|
110
|
+
|
105
|
-
// データベースを削除したい場合はコメントを外します。
|
111
|
+
// データベースを削除したい場合はコメントを外します。
|
106
|
-
|
112
|
+
|
107
|
-
//indexedDB.deleteDatabase("mydb");
|
113
|
+
//indexedDB.deleteDatabase("mydb");
|
108
|
-
|
114
|
+
|
109
|
-
var openRequest = indexedDB.open("mydb", 1.0);
|
115
|
+
var openRequest = indexedDB.open("mydb", 1.0);
|
110
|
-
|
116
|
+
|
111
|
-
openRequest.onupgradeneeded = function(event) {
|
117
|
+
openRequest.onupgradeneeded = function(event) {
|
112
|
-
|
118
|
+
|
113
|
-
// データベースのバージョンに変更があった場合
|
119
|
+
// データベースのバージョンに変更があった場合
|
114
|
-
|
120
|
+
|
115
|
-
db = event.target.result;
|
121
|
+
db = event.target.result;
|
122
|
+
|
116
|
-
|
123
|
+
console.log(db);
|
124
|
+
|
117
|
-
var store = db.createObjectStore("mystore", { keyPath: "mykey",autoIncrement:true});
|
125
|
+
var store = db.createObjectStore("mystore", { keyPath: "mykey",autoIncrement:true});
|
126
|
+
|
127
|
+
console.log(store);
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
openRequest.onsuccess = function(event) {
|
132
|
+
|
133
|
+
db = event.target.result;
|
134
|
+
|
135
|
+
resolve(db);
|
136
|
+
|
137
|
+
console.log(db);
|
138
|
+
|
139
|
+
alert("2データベースを開くことに成功");
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
} else {
|
144
|
+
|
145
|
+
window.alert("このブラウザではIndexed DataBase API は使えません。");
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
})
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
//データ一覧表示
|
160
|
+
|
161
|
+
function displayAlldate() {
|
162
|
+
|
163
|
+
alert("3データ一覧表示");
|
164
|
+
|
165
|
+
var result = document.getElementById("contsPage2id");
|
166
|
+
|
167
|
+
result.innerHTML = "";
|
168
|
+
|
169
|
+
var transaction = db.transaction(["mystore"], "readwrite");
|
170
|
+
|
171
|
+
var store = transaction.objectStore("mystore");
|
172
|
+
|
173
|
+
var request = store.openCursor();
|
174
|
+
|
175
|
+
request.onsuccess = function (event) {
|
176
|
+
|
177
|
+
if(event.target.result == null) {
|
178
|
+
|
179
|
+
return;
|
118
180
|
|
119
181
|
}
|
120
182
|
|
183
|
+
var cursor = event.target.result;
|
184
|
+
|
185
|
+
var data = cursor.value;
|
186
|
+
|
187
|
+
result.innerHTML += "<table border='1'><td>"+"key:" + cursor.key + "</td>" +"<td>"+"value:" + data.myvalue + " value2:" + data.myvalue2 +"</td></table>";
|
188
|
+
|
189
|
+
cursor.continue();
|
190
|
+
|
191
|
+
}
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
//データ入力欄、保存ボタン表示
|
200
|
+
|
201
|
+
function makeInputfield(){
|
202
|
+
|
203
|
+
alert("4データ入力欄表示");
|
204
|
+
|
205
|
+
const result = document.getElementById("contsPage1id");
|
206
|
+
|
207
|
+
if (!result.hasChildNodes()){
|
208
|
+
|
209
|
+
const input1 = document.createElement("input");
|
210
|
+
|
211
|
+
input1.setAttribute("type","text");
|
212
|
+
|
213
|
+
var newvalue = newvalue;
|
214
|
+
|
215
|
+
input1.setAttribute("id","newvalue");
|
216
|
+
|
217
|
+
input1.setAttribute("name","newvalue");
|
218
|
+
|
219
|
+
result.appendChild(input1);
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
const input2 = document.createElement("input");
|
224
|
+
|
225
|
+
input2.setAttribute("type","text");
|
226
|
+
|
227
|
+
var newvalue2 = newvalue2;
|
228
|
+
|
229
|
+
input2.setAttribute("id","newvalue2");
|
230
|
+
|
231
|
+
input2.setAttribute("name","newvalue2");
|
232
|
+
|
233
|
+
result.appendChild(input2);
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
const input3 = document.createElement("input");
|
238
|
+
|
239
|
+
input3.setAttribute("type","text");
|
240
|
+
|
241
|
+
var newkey = newkey;
|
242
|
+
|
243
|
+
input3.setAttribute("id","newkey");
|
244
|
+
|
245
|
+
input3.setAttribute("name","newkey");
|
246
|
+
|
247
|
+
result.appendChild(input3);
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
const input4 = document.createElement("input");
|
252
|
+
|
253
|
+
input4.setAttribute("type","button");
|
254
|
+
|
255
|
+
input4.setAttribute('onclick','saveBtn(\'' + newkey + '\',\'' + newvalue + '\',\'' + newvalue2 + '\');');
|
256
|
+
|
257
|
+
input4.setAttribute("value","保存");
|
258
|
+
|
259
|
+
result.appendChild(input4);
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
}
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
|
268
|
+
|
269
|
+
//データ保存ボタン
|
270
|
+
|
271
|
+
function saveBtn(newkey,newvalue,newvalue2){
|
272
|
+
|
273
|
+
var value = newvalue;
|
274
|
+
|
275
|
+
var value2 = newvalue2;
|
276
|
+
|
277
|
+
var key = newkey;
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
function transactStore(event){
|
282
|
+
|
283
|
+
var transaction = db.transaction(["mystore"], "readwrite");
|
284
|
+
|
285
|
+
var store = transaction.objectStore("mystore");
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
function putStore(event){
|
290
|
+
|
291
|
+
var value = document.getElementById("newvalue").value;
|
292
|
+
|
293
|
+
var value2 = document.getElementById("newvalue2").value;
|
294
|
+
|
295
|
+
var key = document.getElementById("newkey").value;
|
296
|
+
|
297
|
+
var requestupdate = store.put({mykey: key, myvalue: value, myvalue2: value2});
|
298
|
+
|
299
|
+
|
300
|
+
|
121
|
-
|
301
|
+
requestupdate.onsuccess = function(event){
|
122
|
-
|
302
|
+
|
123
|
-
|
303
|
+
document.getElementById("newvalue").value = "";
|
304
|
+
|
124
|
-
|
305
|
+
document.getElementById("newvalue2").value = "";
|
306
|
+
|
307
|
+
document.getElementById("newkey").value = "";
|
308
|
+
|
309
|
+
|
310
|
+
|
125
|
-
alert("
|
311
|
+
alert("保存しました。");
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
displayAlldate();
|
316
|
+
|
317
|
+
}
|
126
318
|
|
127
319
|
}
|
128
320
|
|
129
|
-
|
321
|
+
putStore(event);
|
130
|
-
|
131
|
-
window.alert("このブラウザではIndexed DataBase API は使えません。");
|
132
322
|
|
133
323
|
}
|
134
324
|
|
135
|
-
|
325
|
+
transactStore(event);
|
136
|
-
|
326
|
+
|
137
|
-
}
|
327
|
+
}
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
328
|
+
|
144
|
-
|
145
|
-
function displayAlldate() {
|
146
|
-
|
147
|
-
alert("3データ一覧表示");
|
148
|
-
|
149
|
-
var result = document.getElementById("contsPage2id");
|
150
|
-
|
151
|
-
result.innerHTML = "";
|
152
|
-
|
153
|
-
var transaction = db.transaction(["mystore"], "readwrite");
|
154
|
-
|
155
|
-
var store = transaction.objectStore("mystore");
|
156
|
-
|
157
|
-
var request = store.openCursor();
|
158
|
-
|
159
|
-
request.onsuccess = function (event) {
|
160
|
-
|
161
|
-
if(event.target.result == null) {
|
162
|
-
|
163
|
-
|
329
|
+
saveBtn;
|
164
|
-
|
165
|
-
}
|
166
|
-
|
167
|
-
var cursor = event.target.result;
|
168
|
-
|
169
|
-
var data = cursor.value;
|
170
|
-
|
171
|
-
result.innerHTML += "<table border='1'><td>"+"key:" + cursor.key + "</td>" +"<td>"+"value:" + data.myvalue + " value2:" + data.myvalue2 +"</td></table>";
|
172
|
-
|
173
|
-
cursor.continue();
|
174
|
-
|
175
|
-
}
|
176
|
-
|
177
|
-
}
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
//データ入力欄、保存ボタン表示
|
184
|
-
|
185
|
-
function makeInputfield(){
|
186
|
-
|
187
|
-
alert("4データ入力欄表示");
|
188
|
-
|
189
|
-
const result = document.getElementById("contsPage1id");
|
190
|
-
|
191
|
-
if (!result.hasChildNodes()){
|
192
|
-
|
193
|
-
const input1 = document.createElement("input");
|
194
|
-
|
195
|
-
input1.setAttribute("type","text");
|
196
|
-
|
197
|
-
var newvalue = newvalue;
|
198
|
-
|
199
|
-
input1.setAttribute("id","newvalue");
|
200
|
-
|
201
|
-
input1.setAttribute("name","newvalue");
|
202
|
-
|
203
|
-
result.appendChild(input1);
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
const input2 = document.createElement("input");
|
208
|
-
|
209
|
-
input2.setAttribute("type","text");
|
210
|
-
|
211
|
-
var newvalue2 = newvalue2;
|
212
|
-
|
213
|
-
input2.setAttribute("id","newvalue2");
|
214
|
-
|
215
|
-
input2.setAttribute("name","newvalue2");
|
216
|
-
|
217
|
-
result.appendChild(input2);
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
const input3 = document.createElement("input");
|
222
|
-
|
223
|
-
input3.setAttribute("type","text");
|
224
|
-
|
225
|
-
var newkey = newkey;
|
226
|
-
|
227
|
-
input3.setAttribute("id","newkey");
|
228
|
-
|
229
|
-
input3.setAttribute("name","newkey");
|
230
|
-
|
231
|
-
result.appendChild(input3);
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
const input4 = document.createElement("input");
|
236
|
-
|
237
|
-
input4.setAttribute("type","button");
|
238
|
-
|
239
|
-
input4.setAttribute('onclick','saveBtn(\'' + newkey + '\',\'' + newvalue + '\',\'' + newvalue2 + '\');');
|
240
|
-
|
241
|
-
input4.setAttribute("value","保存");
|
242
|
-
|
243
|
-
result.appendChild(input4);
|
244
|
-
|
245
|
-
}
|
246
|
-
|
247
|
-
}
|
248
330
|
|
249
331
|
|
250
332
|
|
251
|
-
|
252
|
-
|
253
|
-
//データ保存ボタン
|
254
|
-
|
255
|
-
function saveBtn(newkey,newvalue,newvalue2){
|
256
|
-
|
257
|
-
var value = newvalue;
|
258
|
-
|
259
|
-
var value2 = newvalue2;
|
260
|
-
|
261
|
-
var key = newkey;
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
function transactStore(event){
|
266
|
-
|
267
|
-
var transaction = db.transaction(["mystore"], "readwrite");
|
268
|
-
|
269
|
-
var store = transaction.objectStore("mystore");
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
function putStore(event){
|
274
|
-
|
275
|
-
var value = document.getElementById("newvalue").value;
|
276
|
-
|
277
|
-
var value2 = document.getElementById("newvalue2").value;
|
278
|
-
|
279
|
-
var key = document.getElementById("newkey").value;
|
280
|
-
|
281
|
-
var requestupdate = store.put({mykey: key, myvalue: value, myvalue2: value2});
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
requestupdate.onsuccess = function(event){
|
286
|
-
|
287
|
-
document.getElementById("newvalue").value = "";
|
288
|
-
|
289
|
-
document.getElementById("newvalue2").value = "";
|
290
|
-
|
291
|
-
document.getElementById("newkey").value = "";
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
alert("保存しました。");
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
displayAlldate();
|
300
|
-
|
301
|
-
}
|
302
|
-
|
303
|
-
}
|
304
|
-
|
305
|
-
putStore(event);
|
306
|
-
|
307
|
-
}
|
308
|
-
|
309
|
-
transactStore(event);
|
310
|
-
|
311
|
-
}
|
312
|
-
|
313
|
-
saveBtn;
|
314
|
-
|
315
|
-
|
316
|
-
|
317
333
|
</script>
|
318
334
|
|
319
335
|
</body>
|
1
openIndexeddb関数へのpromiseの追加、async/awaitの付与
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,11 +14,25 @@
|
|
14
14
|
|
15
15
|
以下のopenIndexeddb関数内のopenRequest.onsuccess実行後に、displayAlldate関数を実行したいのですが、
|
16
16
|
|
17
|
-
実行前にdisplayAlldate関数が実行されてしまいます。
|
17
|
+
~~実行前にdisplayAlldate関数が実行されてしまいます。
|
18
|
+
|
18
|
-
|
19
|
+
~~
|
19
|
-
|
20
|
-
|
20
|
+
|
21
|
-
エラー:ncaught (in promise) TypeError: Cannot read property 'transaction' of undefined at displayAlldate
|
21
|
+
~~エラー:ncaught (in promise) TypeError: Cannot read property 'transaction' of undefined at displayAlldate
|
22
|
+
|
23
|
+
~~
|
24
|
+
|
25
|
+
|
26
|
+
|
27
|
+
ご指摘頂いた内容にて、
|
28
|
+
|
29
|
+
1,openIndexeddb関数にpromiseを追加
|
30
|
+
|
31
|
+
2,async/awaitでopenIndexeddb関数にawaitを付与しました。
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
ただ、同関数の実行後に、他のdisplayAlldate関数→makeInputfield関数が実行されません。
|
22
36
|
|
23
37
|
|
24
38
|
|
@@ -54,17 +68,21 @@
|
|
54
68
|
|
55
69
|
<script>
|
56
70
|
|
57
|
-
//promiseによる実行順序の制御(openIndexeddb→displayAlldate→makeInputfieldの順番で実行)
|
71
|
+
//promiseによる実行順序の制御(openIndexeddb実行後→displayAlldate→makeInputfieldの順番で実行)
|
72
|
+
|
58
|
-
|
73
|
+
start();
|
74
|
+
|
59
|
-
|
75
|
+
async function start(){
|
60
|
-
|
61
|
-
|
76
|
+
|
62
|
-
|
63
|
-
|
77
|
+
await openIndexeddb();
|
64
|
-
|
78
|
+
|
65
|
-
|
79
|
+
displayAlldate();
|
66
|
-
|
80
|
+
|
67
|
-
|
81
|
+
makeInputfield();
|
82
|
+
|
83
|
+
}
|
84
|
+
|
85
|
+
|
68
86
|
|
69
87
|
|
70
88
|
|
@@ -78,9 +96,11 @@
|
|
78
96
|
|
79
97
|
function openIndexeddb(){
|
80
98
|
|
99
|
+
return new Promise(function(){
|
100
|
+
|
81
|
-
alert("1データベースを開く");
|
101
|
+
alert("1データベースを開く");
|
82
|
-
|
102
|
+
|
83
|
-
if (indexedDB) {
|
103
|
+
if (indexedDB) {
|
84
104
|
|
85
105
|
// データベースを削除したい場合はコメントを外します。
|
86
106
|
|
@@ -112,13 +132,17 @@
|
|
112
132
|
|
113
133
|
}
|
114
134
|
|
135
|
+
});
|
136
|
+
|
115
|
-
}
|
137
|
+
}
|
116
|
-
|
117
|
-
|
138
|
+
|
139
|
+
|
140
|
+
|
141
|
+
|
118
142
|
|
119
143
|
//データ一覧表示
|
120
144
|
|
121
|
-
function displayAlldate(
|
145
|
+
function displayAlldate() {
|
122
146
|
|
123
147
|
alert("3データ一覧表示");
|
124
148
|
|
@@ -152,7 +176,7 @@
|
|
152
176
|
|
153
177
|
}
|
154
178
|
|
155
|
-
|
179
|
+
|
156
180
|
|
157
181
|
|
158
182
|
|
@@ -222,7 +246,7 @@
|
|
222
246
|
|
223
247
|
}
|
224
248
|
|
225
|
-
|
249
|
+
|
226
250
|
|
227
251
|
|
228
252
|
|
@@ -288,9 +312,7 @@
|
|
288
312
|
|
289
313
|
saveBtn;
|
290
314
|
|
291
|
-
|
292
|
-
|
293
|
-
|
315
|
+
|
294
316
|
|
295
317
|
</script>
|
296
318
|
|