1

尝试使用以下函数获取数据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.

应该做些什么不同的事情?

4

1 回答 1

2

您没有return从 中获得承诺db.events.each,因此 undefined 会通过管道传递到您的顶级承诺中。

于 2017-09-15T00:41:11.547 回答