문제

After inserting data using PouchDB I tried db.getAll() to retrieve all the documents and db.get() for single documents but none of the objects returned contained the value I was inserted in.

What am I doing wrong?

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)
  })
})
도움이 되었습니까?

해결책

Your allDocs query is running before you have completed inserting the data into PouchDB, due to the IndexedDB API all database queries are asynchronous (they likely would have to be anyway as it's also a HTTP client).

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)
    });
  });
});

... should work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top