【问题标题】:Unable to retrieve value from PouchDB无法从 PouchDB 中检索值
【发布时间】:2016-12-25 11:29:45
【问题描述】:

使用 PouchDB 插入数据后,我尝试使用 db.getAll() 检索所有文档,并尝试使用 db.get() 检索单个文档,但返回的对象都不包含我插入的值。

我做错了什么?

new Pouch('idb://test', function(err, db) {
  doc = {
    test : 'foo',
    value : 'bar'
  }

  db.post(doc, function(err, data) {
    if (err) console.error(err)
      else console.log(data)
  })

  db.allDocs(function(err, data) {
    if (err) console.error(err)
      else console.log(data)
  })
})

【问题讨论】:

    标签: couchdb indexeddb pouchdb


    【解决方案1】:

    您的 allDocs 查询在您完成将数据插入 PouchDB 之前运行,由于 IndexedDB API,所有数据库查询都是异步的(它们可能必须是异步的,因为它也是一个 HTTP 客户端) .

    new Pouch('idb://test', function(err, db) {
      var doc = {
        test : 'foo',
        value : 'bar'
      };
      db.post(doc, function(err, data){
        if (err) {
          return console.error(err);
        }
        db.allDocs(function(err, data){
          if (err)    console.err(err)
          else console.log(data)
        });
      });
    });
    

    ...应该可以工作。

    【讨论】:

    • 明白了!是的,异步部分导致了它。谢谢:)
    猜你喜欢
    • 2016-11-02
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多