我制作了以下异步函数,它接收一个"id";(integer(值作为参数。
但是当我将1作为"0"的整数值传递时;id";,console.log输出表示";"未定义";,而如果我替换";id";在带有硬编码1的store.get((中,它可以工作,并在console.log中显示正确的输出。我已经尝试了console.log(id(,我可以确认它收到了正确的参数值,在本例中为1。
但我不明白为什么它在收到";id";价值而不是硬编码的?
store.get((函数是标准indexedDB函数的一部分。
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)
}
})
}
我找到了答案。令人困惑的是,当我登录";id";值,则会显示:1就好像这只是一个数字。但如果我做了id===1检查,那就错了。所以我通过解决了这个问题
let numberId = parseInt(id, 10)
之后,我能够在store.get((方法中正确使用该值。