Pergunta

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)
  })
})
Foi útil?

Solução

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top