从 Dexie promise 中获取价值
Getting a value from Dexie promise
正在尝试使用 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}`);
});
显然,我做错了什么。我的承诺以正确的顺序执行。我从 findEvent
内部的日志中正确打印了 snippet
。调用代码中登录result
时为undefined
.
有什么不同之处?
您没有return
使用 db.events.each
的 promise,因此 undefined 会通过管道传输到您的顶级 promise。
正在尝试使用 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}`);
});
显然,我做错了什么。我的承诺以正确的顺序执行。我从 findEvent
内部的日志中正确打印了 snippet
。调用代码中登录result
时为undefined
.
有什么不同之处?
您没有return
使用 db.events.each
的 promise,因此 undefined 会通过管道传输到您的顶级 promise。