0
votes

I try to write an angular app with IndexedDB for a little book-store. I set the index 'isbn' to unique and now I try to provoke the error because I want to tell the User that the isbn has to be unique and want to show the error message.

I can console.log(e) and get following output:

Event

   {isTrusted: true, type: "error", 
   target: IDBRequest, currentTarget: IDBRequest, eventPhase: 2, …}

isTrusted: true
type: "error"
target: IDBRequest
result: undefined

error: DOMException: Unable to add key to index 'isbn': at least one key does not satisfy the uniqueness requirements.

The problem:

I can't write console.log(e.target.error) because TypeScript says: TS2339: Property 'error' does not exist on type 'EventTarget'

Why?

#

This is the method:

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');
};

}

1

1 Answers

3
votes

It's a bug in TypeScript, see https://github.com/microsoft/TypeScript/issues/30669

You can work around it by casting to any or using // @ts-ignore or something.