Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to retrieve value from PouchDB

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)
  })
})
like image 698
andrei Avatar asked Jan 16 '23 20:01

andrei


1 Answers

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.

like image 143
Dale Harvey Avatar answered Jan 31 '23 11:01

Dale Harvey