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
150
151
|
---
title: IDBRequest.onerror
slug: Web/API/IDBRequest/onerror
translation_of: Web/API/IDBRequest/onerror
---
<p>{{ APIRef("IndexedDB") }}</p>
<p>le gestionnaire d'événement <strong><code>onerror</code></strong> de l'interface {{domxref("IDBRequest")}} capte l'événement <code><a href="/en-US/docs/Web/Events/error">error</a></code>, déclenché quand une requête renvoie une erreur.</p>
<p>Le gestionnaire d'événement reçoit l'événement <a href="/en-US/docs/Web/Events/error">error</a> avec le type="error" en paramètre.</p>
<p>{{AvailableInWorkers}}</p>
<h2 id="Syntaxe">Syntaxe</h2>
<pre class="brush: js">request.onerror = function( event ) { ... };</pre>
<h2 id="Exemple">Exemple</h2>
<p>L'exemple suivant demande un titre d'enregistrement donné, <code>onsuccess</code> obtient l'enregistrement associé au {{domxref("IDBObjectStore","magasin d'objets")}} (mis à disposition en tant que <code style="font-style: normal;">objectStoreTitleRequest.result</code>), on met à jour une propriété de l'enregistrement, puis le sauve dans le magasin d'objets. En bas le gestionnaire d'événement <code>onerror</code> affiche le code d'erreur si la {{domxref("IDBRequest","requête")}} échoue. Pour un exemple de travail complet, voir notre <a href="https://github.com/mdn/to-do-notifications/">To-do Notifications</a> app (<a href="http://mdn.github.io/to-do-notifications/">view example live</a>).</p>
<pre>var title = "Walk dog";
// Ouvrez une transaction comme d'habitude
var objectStore = db.transaction(['toDoList'], "readwrite").objectStore('toDoList');
// Obtenez l'objet toDoList qui a ce titre
var objectStoreTitleRequest = objectStore.get(title);
objectStoreTitleRequest.onsuccess = function() {
// Prenez l'objet de données renvoyé comme résultat
var data = objectStoreTitleRequest.result;
// Mettre à jour la valeur notified de l'objet à "yes"
data.notified = "yes";
// Créer une autre requête qui insère le nouvelle élément dans la base de données
var updateTitleRequest = objectStore.put(data);
// Lorsque cette requête réussit, appelle de la fonction displayData() pour mettre à jour l'affichage
updateTitleRequest.onsuccess = function() {
displayData();
};
};
objectStoreTitleRequest.onerror = function() {
// Si une erreur se produit à la demande, on l'affiche
console.log("Il y a eu une erreur dans la récupération des données: " + objectStoreTitleRequest.error);
};
</pre>
<h2 id="Spécifications">Spécifications</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Spécification</th>
<th scope="col">Statut</th>
<th scope="col">Commentaire</th>
</tr>
<tr>
<td>{{SpecName('IndexedDB', '#widl-IDBRequest-onerror', 'onerror')}}</td>
<td>{{Spec2('IndexedDB')}}</td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="Browser_compatibility" name="Browser_compatibility">Compatibilité avec les navigateurs</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
<table class="compat-table">
<tbody>
<tr>
<th>Fonctionnalité</th>
<th>Chrome</th>
<th>Firefox (Gecko)</th>
<th>Internet Explorer</th>
<th>Opera</th>
<th>Safari (WebKit)</th>
</tr>
<tr>
<td>Support basique</td>
<td>23{{property_prefix("webkit")}}<br>
24</td>
<td>10 {{property_prefix("moz")}}<br>
{{CompatGeckoDesktop("16.0")}}</td>
<td>10, en partie</td>
<td>15</td>
<td>7.1</td>
</tr>
<tr>
<td>Disponible dans workers</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("37.0")}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<div id="compat-mobile">
<table class="compat-table">
<tbody>
<tr>
<th>Fonctionnalité</th>
<th>Android</th>
<th>Firefox Mobile (Gecko)</th>
<th>Firefox OS</th>
<th>IE Phone</th>
<th>Opera Mobile</th>
<th>Safari Mobile</th>
</tr>
<tr>
<td>Support basique</td>
<td>4.4</td>
<td>{{CompatGeckoMobile("22.0")}}</td>
<td>1.0.1</td>
<td>10</td>
<td>22</td>
<td>8</td>
</tr>
<tr>
<td>Disponible dans workers</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatGeckoMobile("37.0")}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
<td>{{CompatVersionUnknown}}</td>
<td>{{CompatUnknown}}</td>
</tr>
</tbody>
</table>
</div>
<h2 id="Voir_aussi">Voir aussi</h2>
<ul>
<li><a href="/en-US/docs/Web/API/IndexedDB_API/Using_IndexedDB">Using IndexedDB</a></li>
<li><a href="/en-US/docs/Web/Events/error">Error Event</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>
|