Learning to do "queries" for PouchDB

845 views
Skip to first unread message

Steve Husting

unread,
Jan 12, 2015, 5:57:05 PM1/12/15
to pou...@googlegroups.com
After a week, I'm still trying to nail down how to get the contents filtered. I have compiled the following, but it still doesn't output via innerHTML. Can anyone point out the error in the code? 

PouchDB.destroy('vehicles').then(function() {
       return new PouchDB('vehicles');
    }).then(function(db) {

        db.bulkDocs([{
...
        }
        ]);
});


function findDrive(drive, callback) {
    // emit documents
    function myMapFunction(doc) {
        if (doc.drive === '2WD') {
            emit(doc.drive, null);
        }
    }

  // filter results
  function filter(err, response) {
    if (err) return callback(err);

    var matches = [];
    response.rows.forEach(function(vehicles) {
      if (vehicles.drive === '2WD') {
        matches.push(vehicles);
        document.getElementById('display').innerHTML += doc.drive + ', ' + doc.name1 + ', ' + doc.scale + ', ' + doc.body1 + '<br>';
      }
    });
    callback(null, matches);
  }

  // kick off the PouchDB query with the map & filter functions above
  db.query({map: map}, {reduce: false}, filter)
}

I'm getting the error:
pouchdb:api vehicles +6ms get error r {status: 404, name: "not_found", message: "missing", error: true, toString: function}

step...@shimaore.net

unread,
Jan 13, 2015, 5:44:42 AM1/13/15
to pou...@googlegroups.com
Hi Steve,

I'm kind of confused about how you get access to the `db` in the
first place in your example. I'm going to assume something like:

var db = null;

/* Initialize the database */

var initial_records = [ /* your data here */ ];
PouchDB.destroy('vehicles')
.then(function() {
db = new PouchDB('vehicles');
return db.bulkDocs(initial_records);
})
.then(database_is_ready());

What I mean is that you need to make sure the database is initialized
before you start using it, which can only happen for sure in a `.then`
after your `bulkDocs` command.

Also notice how I `return` the Promise that `db.bulkDocs` generates -- this
ensures sequentiality.

/* Use the database */

function database_is_ready() {
// main body for the application... at some point:
var user_selected_drive = get_user_selected_drive();
findDrive(user_selected_drive)
.then(showDrive);
});

Notice how I use `.then` here again; this means that our `findDrive()`
will need to return a Promise. (More on this later.)

This is the map function:

function by_drive(doc) {
emit(doc.drive, null);
}

You'll notice that the map emits for all documents; the key of the index
is the `drive` field. Then in `db.query` we simply ask for that key,
there's no need to do an additional comparison in the display function.
The other thing is that you need to add `include_docs` if you want to
see any documents in the results.

Then for your `map` to be meaningful you're probably looking into
something which actually provides `doc` and `map` variables (I don't see
how they would get populated in your example) and uses the `drive`
variable provided to the `findDrive()` function.

function findDrive(drive) {
return db.query({map: by_drive}, {key: drive, include_doc:true})
.then(function (response) {
return response.rows.map(function(row){return row.doc});
});
}

Notice how `findDrive()` returns the Promise created by `db.query`.

Finally here's the display function:

function showDrive(matches) {
matches.forEach(function(row) {
document.getElementById('display').innerHTML +=
row.doc.drive + ', ' + // etc.
}

It's essentially identical to the one you showed minus the array-building.

You'll also notice I didn't check for errors. If you want to do that,
a good place to do that would be in a

.catch(function(error){...})

right after the line that says `.then(showDrive)` in `database_is_ready`.

I haven't tested any of this code so don't assume it'll work out of the
box, but HTH.
S.

--
tel:+33643482771
http://stephane.shimaore.net/

Steve Husting

unread,
Jan 13, 2015, 11:34:13 AM1/13/15
to pou...@googlegroups.com
Hi Stephane,

Thank you for your extended answer! I'll start trying to work it into my example. 

As to your confusion about the way I began the code, the base code comes from http://bl.ocks.org/nolanlawson/038a45134341f3b7235b, which is a very simple implementation of CouchDB and works fine. Then from there I tried to figure out how to map/reduce to output exactly what I wanted. The OP is what I ended up with. A mess, I see now! I'll post back with any problems I'm having. 

Thanks again,
Steve H
Message has been deleted
Message has been deleted

Nolan Lawson

unread,
Jan 13, 2015, 4:12:11 PM1/13/15
to pou...@googlegroups.com
Hi Steve,

The guide to map/reduce may also be helpful :)

http://pouchdb.com/guides/queries.html

Map/reduce is definitely a bit complicated, though. We are working on a simplified API.

Cheers,
Nolan

Steve Husting

unread,
Jan 13, 2015, 4:21:52 PM1/13/15
to pou...@googlegroups.com
Nolan,

Already read it. Unfortunately, it's written for highly experienced users of JavaScript, which isn't me.

Steve Husting

unread,
Jan 14, 2015, 11:01:30 AM1/14/15
to pou...@googlegroups.com
Nolan,

Here's a good example of helping out those who are not yet JS experts:

Thanks for helping! 

Nolan Lawson

unread,
Jan 15, 2015, 11:33:26 AM1/15/15
to pou...@googlegroups.com
Yep, that's why we are working on the simplified version. :)

Steve Husting

unread,
Jan 15, 2015, 11:56:45 AM1/15/15
to pou...@googlegroups.com
Great! I'll have to abandon this for now, and I'll be watching for the new version.
Reply all
Reply to author
Forward
0 new messages