teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

コードを掲載します

2016/09/14 00:45

投稿

退会済みユーザー
answer CHANGED
@@ -4,11 +4,71 @@
4
4
  OKの時にあるはずの無い次のカーソルへ進めているのでNGにもなっているのではないでしょうか。
5
5
  `cursor.continue();`を消してはどうでしょう。
6
6
 
7
- `alertShowForBarcode0Event`を`console.log`に置き換えて
8
- CSVから読み込んだものとして仮に`csvArray = [[1,'a','A'],[2,'b','B']]`を行った後、
9
- `itemCheckTest('a','A');`呼び出すと次ようになりまし
7
+ - Chumiさんのコード一部変更して動かしてみたもをはっておきすね、私の勘違いもあるかもれませんので
10
8
 
9
+ ```html
10
+ <html>
11
+ <head>
12
+ <script>
13
+ function itemCheckTest(barcode1, barcode2) {
14
+
15
+ csvArray = [[1,'a','A'],[2,'b','B']]
16
+
17
+ //1.indexedDB関連オブジェクトの取得
18
+ var indexedDB = window.indexedDB || window.webkitIndexedDB || window.mozIndexedDB || window.msIndexedDB;
19
+ var IDBTransaction = window.IDBTransaction || window.webkitIDBTransaction || window.mozIDBTransaction || window.msIDBTransaction;
20
+ var IDBKeyRange = window.IDBKeyRange || window.webkitIDBKeyRange || window.mozIDBKeyRange || window.msIDBKeyRange;
21
+ var IDBCursor = window.IDBCursor || window.webkitIDBCursor;
22
+ //2.indexedDBを開く
23
+ var idbReq = indexedDB.open("ItemDB", 1);
24
+ //3.DBの新規作成時、またはバージョン変更時に実行するコード
25
+ idbReq.onupgradeneeded = function (event) {
26
+ var db = event.target.result;
27
+ var itemStore = db.createObjectStore("item", { keyPath: "item_id" });
28
+ itemStore.createIndex("bc", ["bc1","bc2"], { unique: false });
29
+ console.log("インデックス追加しました");
30
+ //データの追加
31
+ for (var i = 0, len = csvArray.length; i < len; i++) {
32
+ var csvArrayIn = csvArray[i];
33
+ itemStore.add({ item_id: csvArrayIn[0], bc1:csvArrayIn[1], bc2:csvArrayIn[2]});
34
+ csvArrayIn = [];
35
+ }
36
+ console.log("データ追加しました");
37
+ }
38
+ //4-1.DBオープン失敗時の処理
39
+ idbReq.onerror = function (event) {
40
+ console.log("error");
41
+ };
42
+ //4-2.DBオープン成功時の処理
43
+ var db;
44
+ idbReq.onsuccess = function (event) {
45
+ db = idbReq.result;
46
+ //読み書き権限付きで使用することを宣言
47
+ var transaction = db.transaction(["item"], "readwrite");
48
+ //各オブジェクトストアの取り出し
49
+ var itemStore = transaction.objectStore("item");
50
+ var indexBc = itemStore.index("bc");
51
+ var range = IDBKeyRange.only([barcode1,barcode2]);
52
+ indexBc.openCursor(range).onsuccess = function (event) {
53
+ var cursor = event.target.result;
54
+ if (cursor == null) {
55
+ console.log("NG:" + cursor);
56
+ } else {
57
+ console.log("OK:" + cursor);
58
+ cursor.continue();
59
+ }
60
+ }
61
+ }
62
+ }
63
+ itemCheckTest('a','A');
64
+ </script>
65
+ </head>
66
+ </html>
11
67
  ```
68
+
69
+ Chrome53では次のような結果でした。
70
+
71
+ ```
12
72
  OK:[object IDBCursorWithValue]
13
73
  NG:null
14
74
  ```