Why does indexeddb store.get not retrieve anything when using value from parameter, but works with same value as param when hardcoded?

Viewed 15

I have made the following async function, which receives an "id" (integer) value as parameter.

But when I pass 1 as integer value for "id", the console.log output says "undefined", while if I instead replace the "id" in store.get() with a hardcoded 1, it works and shows correct output in console.log. I've tried console.log(id) and I can confirm that it receives the correct parameter value, which in this case is 1.

But I can't figure out why it acts differently when receiving the "id" value rather than a hardcoded one?

The store.get() function is part of standard indexedDB functions.

async getCollection(id) {
        let db = await idb.getDb()
        let trans = db.transaction(['collections'], 'readonly')
        let store = trans.objectStore('collections')
        let collection = store.get(id)

        return new Promise(resolve => {
            console.log(collection)
            collection.onsuccess = () => {
                console.log(collection.result)
            }
        })
    }
1 Answers

I figured the answer. Confusion is, that when I logged the "id" value, it would show: 1 As if it was just a number. But if I did id === 1 check, it would be false. So I solved the issue by

let numberId = parseInt(id, 10)

After which I was able to use that value correctly in the store.get() method.

Related