问题
I have been getting this warning on my application that is using IndexedDB.
"Numeric transaction modes are deprecated in IDBDatabase.transaction. Use "readonly" or "readwrite""
which I didn't see that when I first wrote the app (about few months ago) but appear to get this warning every time it tries to access IDBDatabase.transaction.
on chrome console, it can properly recognize the following transaction key.
IDBTransaction.READ_WRITE
1
IDBTransaction.READ_ONLY
0
my example code that is doing IDB transaction:
IndexedDB.set = function(key, obj, onsuccess, oncomplete) {
var db = IndexedDB.db;
var trans = db.transaction([key], IDBTransaction.READ_WRITE);
var objectStore = trans.objectStore(key);
var request = objectStore.put(obj);
request.onsuccess = function(e) {
if (onsuccess !== undefined)
onsuccess(request.result);
};
request.onerror = function(e) {
console.log("Database error: " + e.target.errorCode);
};
trans.oncomplete = function(e) {
if (oncomplete !== undefined)
oncomplete(request.result);
};
};
should I have to worry about this? if so, how can I avoid this warning?
my chrome v: Version 21.0.1180.75
thanks for your comments.
回答1:
The IndexedDB standard is still a work in progress (although version 1 is very close to finished), and one of the recent changes was switching from the numeric constants like "IDBTransaction.READ_WRITE" to just plain strings like "readwrite". It's a good idea because it makes code more concise and more readable.
The old constants will likely continue to work for some time, albeit with a warning message like you observed, but using the strings is the "correct" way to do it now and that's probably what you should use, like:
var trans = db.transaction([key], "readwrite");
回答2:
For backwards compatibility I would advice to provide a fallback. For chrome, I would ignore the warning you get and stay using the the interface.
Maybe try the following work around to make it backwords and forwards compatible:
var transactionType = { READ_ONLY: "readonly", READ_WRITE: "readwrite" }
if (IDBTransaction.READ_ONLY && IDBTransaction.READ_WRITE){
transactionType.READ_ONLY = IDBTransaction.READ_ONLY
transactionType.READ_WRITE = IDBTransaction.READ_WRITE
}
var trans = db.transaction([key], transactionType.READ_WRITE);
With this solution, you make sure old browser will still work and new one's too.
来源:https://stackoverflow.com/questions/11974115/numeric-transaction-modes-are-deprecated-in-idbdatabase-transaction-use-readon