我尝试使用IndexedDB为一个小书店编写一个有角度的应用程序。我将索引'isbn'设置为unique,现在我尝试引发该错误,因为我想告诉用户isbn必须是唯一的并希望显示错误消息。
我可以console.log(e)并获得以下输出:
事件
{isTrusted: true, type: "error",
target: IDBRequest, currentTarget: IDBRequest, eventPhase: 2, …}
isTrusted: true
type: "error"
target: IDBRequest
result: undefined
错误:DOMException:无法向索引'isbn'添加密钥:至少一个密钥不满足唯一性要求。
问题:
我不能写console.log(e.target.error)] >>,因为TypeScript说:TS2339:类型“ EventTarget”上不存在属性“错误” 为什么?
这是方法:
addItem(){
const request = window.indexedDB.open(this.database.name); request.onsuccess = event => { const item = { title: '', isbn: 1, descrition: '', rating: 1 }; const transaction = request.result.transaction(['books'], 'readwrite'); const objectStore = transaction.objectStore('books'); const objectStoreRequest = objectStore.add(item); objectStoreRequest.onerror = e => { console.log(e.target.error); // Here is the error TS2339: Property does not exist }; }; request.onerror = event => { console.log('Error adding item'); };
}
我尝试使用IndexedDB为一个小书店编写一个有角度的应用程序。我将索引'isbn'设置为唯一,现在尝试引发错误,因为我想告诉用户isbn必须是唯一的...
这是TypeScript中的错误,请参见https://github.com/microsoft/TypeScript/issues/30669
您可以通过强制转换为any
或使用// @ts-ignore
等来解决它。