尝试使用以下函数获取数据Dexie.JS
:
function findEvent(key) {
let snippet = '';
if (!db.isOpen()) {
lastEvent = lastEvent.then(() => db.open());
}
lastEvent = lastEvent.then(() => db.transaction('r', db.events, () => {
db.events.each((element) => {
let d = element.cause.data;
if (d.hasOwnProperty('deleted') && (false == d.deleted) && d.hasOwnProperty('abbreviation') &&
d.hasOwnProperty('contents') && (d.abbreviation == key)) {
console.log(`found matching snippet for key: ${key}, contents: ${d.contents}`);
snippet = d.contents;
}
}).then(() => {
console.log('search callback returning:', snippet);
return snippet;
});
}));
return lastEvent;
}
我称之为:
return findEvent(request.key).then((result) => {
console.log(`findEvent return: ${result}`);
});
显然,我做错了什么。我的承诺以正确的顺序执行。我snippet
从内部正确打印在日志中findEvent
。当result
登录调用代码时,它是undefined
.
应该做些什么不同的事情?