无法从PouchDB中检索值

and*_*rei 4 couchdb indexeddb pouchdb

使用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)
  })
})
Run Code Online (Sandbox Code Playgroud)

Dal*_*vey 9

在完成将数据插入PouchDB之前,您的allDocs查询正在运行,因为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)
    });
  });
});
Run Code Online (Sandbox Code Playgroud)

... 应该管用.