{"id":8094,"date":"2015-11-26T13:37:34","date_gmt":"2015-11-26T13:37:34","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-query-using-parameters-open-source-projects-pouchdb-pouchdb\/"},"modified":"2022-08-30T15:03:01","modified_gmt":"2022-08-30T15:03:01","slug":"pouchdb-query-using-parameters-open-source-projects-pouchdb-pouchdb","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/26\/pouchdb-query-using-parameters-open-source-projects-pouchdb-pouchdb\/","title":{"rendered":"PouchDB query using parameters-open source projects pouchdb\/pouchdb"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/37d1b6b293bc162ef4e666de31fb3d53?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>chesles<\/strong><\/p>\n<p>Your <code>map<\/code> function loses its closure because it is re-evaluated inside of PouchDB (that&#8217;s how it gets the <code>emit<\/code> function). This means you can&#8217;t access any variables from your code, but you can still query the database.<\/p>\n<p>In PouchDB, views are not persistent, so your query always looks at every document in the database, and you have to do the filtering <em>after<\/em> the map function. Something like this:<\/p>\n<pre><code>function findCars(horsePower, callback) {\n  \/\/ emit car documents\n  function map(doc) {\n    if(doc.type == 'car' &amp;&amp; doc.value) {\n      emit(doc.value, null);\n    }\n  }\n\n  \/\/ filter results\n  function filter(err, response) {\n    if (err) return callback(err);\n\n    var matches = [];\n    response.rows.forEach(function(car) {\n      if (car.hp == horsePower) {\n        matches.push(car);\n      }\n    });\n    callback(null, matches);\n  }\n\n  \/\/ kick off the PouchDB query with the map &amp; filter functions above\n  db.query({map: map}, {reduce: false}, filter)\n}\n<\/code><\/pre>\n<p>Is one way to solve this problem. Pouch will iterate over each document, passing it to your <code>map<\/code> function. When done, <code>filter<\/code> gets called with an array of all the emitted documents. <code>filter<\/code> does not lose its closure context, so you can filter the results based on horsepower or any other field here.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>chesles Your map function loses its closure because it is re-evaluated inside of PouchDB (that&#8217;s how it gets the emit function). This means you can&#8217;t access any variables from your code, but you can still query the database. In PouchDB, views are not persistent, so your query always looks at every document in the database, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8094","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8094","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8094"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8094\/revisions"}],"predecessor-version":[{"id":8629,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8094\/revisions\/8629"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8094"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8094"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8094"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}