【问题标题】:DexieJS return NotFoundError in a nested queryDexie JS 在嵌套查询中返回 Not Found Error
【发布时间】:2020-11-29 18:55:51
【问题描述】:

当我有一个嵌套查询时,我在 DexieJS 上遇到了一个问题。以下是我的代码示例:-

    let id = 1;

    let child = db.child.where({ id : result.child_id }).first( item => item ).catch( e => "NotFoundError"; );
    console.log(child); // success, it shows the child item

db.parent.where({ id : id }).each( parent => {
  let child = db.child.where({ id : parent.id }).first( item => item ).catch( e => "NotFoundError"; );
  console.log(child); // it will show NotFoundError
});

不知道发生了什么,嵌套查询在 DexieJS 上不起作用,我毫不怀疑它应该起作用。

请指教,提前谢谢。

【问题讨论】:

    标签: dexie dexiejs


    【解决方案1】:

    找到了解决方案或变通方法,它现在正在工作。

    await db.transaction('r', db.parent, db.child, async () => {
      await db.parent.where({ id : id }).each( async parent => {
        let child = db.child.where({ parent_id : parent.id }).first( result => result );
        console.log(child) // *now it shows the child record
      });
    });
    

    看起来我们需要使用事务来在查询结果中进行查询。 我希望这是正确的方法,希望对您有所帮助。 如果这不是正确的方法,请不要犹豫。

    名字。

    【讨论】:

    • 这是一个不错的解决方案。您需要这样做的原因是 each() 在仅包含 db.parent 的隐式只读事务中触发其回调。实际发生的是 db.child 不是事务的一部分。但是,为了简单性和性能,我建议使用 toArray() 而不是 each()。见dexie.org/docs/Collection/Collection.each()#notes
    • @DavidFahlander 感谢您的建议,它有效,应该会更好。我仍然让我崩溃的是以下代码,它不返回结果。 console.log( db.table.where( { id : id }).first( result => result ); 它总是返回一些 De { _listener ........ 我怎样才能得到结果中的对象?谢谢提前
    • 你传递给 console.log() 的是一个承诺。您必须等待它,否则将代码反转为 db.table.where({id: id}).first(result => console.log(result))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-24
    • 2020-01-15
    • 2019-01-19
    • 2020-06-11
    • 2018-03-05
    相关资源
    最近更新 更多