泛舟湖上清波郎朗
2021-10-29 10:53:28
我如何訪問(wèn)id我已經(jīng)添加的項(xiàng)目,因?yàn)檫@些項(xiàng)目偶爾會(huì)被刪除,增加的 id 不統(tǒng)一。我已經(jīng)按照文檔中的方法嘗試過(guò) var db = new Dexie('FRIENDS'); db.version(1).stores({ friends: '++id,name,age,loves' }); db.friends.add({ name: 'user', age: 23, loves:'water', }).then(function(){ return db.friends.get('user'); }).then(function (res) { console.log(res.id); }).catch(function(error) { console.log ("Ooops: " + error); });}我得到 undifined
2 回答

明月笑刀無(wú)情
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊
正確答案是:created id 將作為promise 中的參數(shù)傳遞。文檔中的示例:
db.table('todos')
.add(todo)
.then((id) => {
// ...
});
或者以 async-await 樣式返回:
const id = await db.table('todo').add(todo);
在您的示例中,您使用了錯(cuò)誤的.get()方法語(yǔ)法。正確的是:
table.get({keyPath1: value1, keyPath2: value2, ...});
// e.g.
table.get({id: 15})
// or
db.friends.add({
name: 'user',
age: 23,
loves:'water',
}).then(function(id){
return db.friends.get({id});
})
添加回答
舉報(bào)
0/150
提交
取消