IDBRequest: thuộc tính result
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
Note: This feature is available in Web Workers.
Thuộc tính chỉ đọc result của giao diện IDBRequest trả về kết quả của request.
Giá trị
any
Ngoại lệ
InvalidStateErrorDOMException-
Được ném ra khi cố truy cập thuộc tính trong lúc request chưa hoàn tất, nên kết quả chưa khả dụng.
Ví dụ
Ví dụ sau yêu cầu một tiêu đề bản ghi nhất định, onsuccess lấy bản ghi liên quan từ IDBObjectStore (có sẵn dưới dạng objectStoreTitleRequest.result), cập nhật một thuộc tính của bản ghi, rồi đưa bản ghi đã cập nhật trở lại object store. Để xem một ví dụ hoạt động đầy đủ, hãy xem ứng dụng To-do Notifications của chúng tôi (Xem ví dụ trực tiếp).
js
const title = "Walk dog";
// Mở transaction như bình thường
const objectStore = db
.transaction(["toDoList"], "readwrite")
.objectStore("toDoList");
// Lấy object trong to-do list có tiêu đề này
const objectStoreTitleRequest = objectStore.get(title);
objectStoreTitleRequest.onsuccess = () => {
// Lấy object dữ liệu được trả về như kết quả
const data = objectStoreTitleRequest.result;
// Cập nhật giá trị notified trong object thành "yes"
data.notified = "yes";
// Tạo một request khác để chèn mục
// trở lại cơ sở dữ liệu
const updateTitleRequest = objectStore.put(data);
// Khi request mới này thành công, chạy lại hàm displayData()
// để cập nhật hiển thị
updateTitleRequest.onsuccess = () => {
displayData();
};
};
Thông số kỹ thuật
| Specification |
|---|
| Indexed Database API 3.0> # ref-for-dom-idbrequest-result①> |
Tương thích trình duyệt
Xem thêm
- Dùng IndexedDB
- Bắt đầu transaction:
IDBDatabase - Dùng transaction:
IDBTransaction - Thiết lập một phạm vi khóa:
IDBKeyRange - Truy xuất và thay đổi dữ liệu của bạn:
IDBObjectStore - Dùng cursor:
IDBCursor - Ví dụ tham khảo: To-do Notifications (Xem ví dụ trực tiếp).