add
Summary
Adds a record to the specified object store.
Method of apis/indexeddb/IDBObjectStoreapis/indexeddb/IDBObjectStore
Syntax
var idbRequest = objectStore.add(value, key);
Parameters
value
- Data-type
- String
The value must be valid for the Structured Cloning Algorithm.
key
- Data-type
- String
(Optional)
A key must be provided if the Object Store does not have a key path, or a key generator is not specified.
Return Value
Returns an object of type DOM NodeDOM Node
Examples
var dbOpenRequest = window.indexedDB.open("BookShop1");
dbOpenRequest.onsuccess = function(event){
var db = dbOpenRequest.result;
var transaction = db.transaction(["ObjectStore_BookList"], IDBTransaction.READ_WRITE);
// The object store has a keyPath as "id". Hence no key is specified, but the value has id as a property
var objectStore = transaction.objectStore("ObjectStore_BookList");
// Sample data to add to the Object store
var key = 10;
var value = {
"bookName": "Book-" + Math.random(),
"author": "AuthorName",
"id": key
};
var request = objectStore.add(value);
request.onsuccess = function(event){
console.log("Saved id ", request.result);
};
request.onerror = function(e){
console.log("Error adding data")
};
};
Usage
An error is thrown if one of the following is true
- The object store uses in-line keys and the key parameter was provided.
- The object store uses out-of-line keys and has no key generator and the key parameter was not provided.
- The object store uses in-line keys and the result of evaluating the object store’s key path yields a value and that value is not a valid key.
- The object store uses in-line keys but no key generator and the result of evaluating the object store’s key path does not yield a value.
- The key parameter was provided but does not contain a valid key.
Notes
Remarks
This method can throw the following DOMException
Related specifications
- W3C IndexedDB Specification
- W3C Proposed Recommendation
Attributions
Microsoft Developer Network: [Windows Internet Explorer API reference Article]