aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/idbobjectstore/get/index.html
blob: fb7938d827ad8e3a8814650a6d7e76be1e054512 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
---
title: IDBObjectStore.get()
slug: Web/API/IDBObjectStore/get
translation_of: Web/API/IDBObjectStore/get
---
<p>{{ APIRef("IndexedDB") }}</p>

<div>
<p>{{domxref("IDBObjectStore")}} 的接口 <strong><code>get()</code></strong>方法 返回 {{domxref("IDBRequest")}} 对象,并在“单独的线程(separate thread)”中返回由指定键选择的“对象储存(object store)” 。这用于从对象储存检索特定记录。</p>
</div>

<p>如果成功找到值,则会创建其值的结构化克隆,并设置为“请求对象(request object)”的 <code><a href="/en/IndexedDB/IDBRequest#attr_result" title="en/IndexedDB/IDBRequest#attr result">result</a></code></p>

<p>{{ Note("This method produces the same result for: a) a record that doesn't exist in the database and b) a record that has an undefined value. To tell these situations apart, call the <code>openCursor()</code> method with the same key. That method provides a cursor if the record exists, and no cursor if it does not.") }}</p>

<p>{{AvailableInWorkers}}</p>

<h2 id="语法">语法</h2>

<pre class="syntaxbox notranslate">var <em>request</em> = <em>objectStore</em>.get(<em>key</em>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt>key</dt>
 <dd><span style="">标识要检索的记录的键或键范围</span></dd>
</dl>

<h3 id="返回值">返回值</h3>

<p><span style="">触发与此操作相关的后续事件的</span> {{domxref("IDBRequest")}} 对象。</p>

<h3 id="异常"><span style="">异常</span></h3>

<p><span style="">此方法可能会引发以下类型之一的</span> <span style="line-height: 1.5;"> {{domxref("DOMException")}} :</span></p>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><font face="Courier New, Andale Mono, monospace"><span style="line-height: normal;">TransactionInactiveError</span></font></td>
   <td>This {{domxref("IDBObjectStore")}}'s transaction is inactive.</td>
  </tr>
  <tr>
   <td><font face="Courier New, Andale Mono, monospace"><span style="line-height: normal;">DataError</span></font></td>
   <td>
    <p>The key or key range provided contains an invalid key.</p>
   </td>
  </tr>
  <tr>
   <td><code>InvalidStateError</code></td>
   <td>The {{domxref("IDBObjectStore")}} has been deleted or removed.<br>
     </td>
  </tr>
 </tbody>
</table>

<h2 id="例子">例子</h2>

<p>在以下的代码段中,我们在数据库上打开一个“读/写 事务(read/write transaction)”,并使用 <code>get()</code> 从“对象储存( object store )”中获取一个特定的记录——一个带有“Walk dog”键的示例记录。一旦检索到这个数据对象,你就可以使用普通的JavaScript更新它,然后使用 {{domxref("IDBObjectStore.put")}} 操作将其放回数据库。有关完整的工作示例,查看我们的 <a href="https://github.com/mdn/to-do-notifications/" style="line-height: 1.5;">To-do Notifications</a><span style="line-height: 1.5;"> app (</span><a href="http://mdn.github.io/to-do-notifications/" style="line-height: 1.5;">view example live</a><span style="line-height: 1.5;">.)</span></p>

<pre class="brush: js notranslate">// Let us open our database
<span style="line-height: 1.5;">var DBOpenRequest = window.indexedDB.open("toDoList", 4);

</span><span style="line-height: 1.5;">DBOpenRequest.onsuccess = function(event) {</span>
  note.innerHTML += '&lt;li&gt;Database initialised.&lt;/li&gt;';

  // store the result of opening the database in the db variable.
  // This is used a lot below
  db = DBOpenRequest.result;

  // Run the getData() function to get the data from the database
  getData();
};

function getData() {
  // open a read/write db transaction, ready for retrieving the data
  var transaction = db.transaction(["toDoList"], "readwrite");

  // report on the success of the transaction completing, when everything is done
  transaction.oncomplete = function(event) {
    note.innerHTML += '&lt;li&gt;Transaction completed.&lt;/li&gt;';
  };

  transaction.onerror = function(event) {
    note.innerHTML += '&lt;li&gt;Transaction not opened due to error: ' + transaction.error + '&lt;/li&gt;';
  };

  // create an object store on the transaction
  var objectStore = transaction.objectStore("toDoList");

  // Make a request to get a record by key from the object store
  var objectStoreRequest = objectStore.get("Walk dog");

  objectStoreRequest.onsuccess = function(event) {
    // report the success of our request
    note.innerHTML += '&lt;li&gt;Request successful.&lt;/li&gt;';

    var myRecord = objectStoreRequest.result;
  };

};</pre>

<h2 id="规范"><span style="font-size: 2.14285714285714rem;">规范</span></h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('IndexedDB', '#dom-idbobjectstore-get', 'get()')}}</td>
   <td>{{Spec2('IndexedDB')}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("IndexedDB 2", "#dom-idbobjectstore-get", "get()")}}</td>
   <td>{{Spec2("IndexedDB 2")}}</td>
   <td></td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility" name="Browser_compatibility">浏览器兼容性</h2>

<div>


<p>{{Compat("api.IDBObjectStore.get")}}</p>
</div>

<h2 id="另请参阅">另请参阅</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
 <li>Starting transactions: {{domxref("IDBDatabase")}}</li>
 <li>Using transactions: {{domxref("IDBTransaction")}}</li>
 <li>Setting a range of keys: {{domxref("IDBKeyRange")}}</li>
 <li>Retrieving and making changes to your data: {{domxref("IDBObjectStore")}}</li>
 <li>Using cursors: {{domxref("IDBCursor")}}</li>
 <li>Reference example: <a class="external" href="https://github.com/mdn/to-do-notifications/tree/gh-pages">To-do Notifications</a> (<a class="external" href="http://mdn.github.io/to-do-notifications/">view example live</a>.)</li>
</ul>