Reputation: 23
In CouchDB I have created a view called "zip", the map looks like this;
function (doc) {
if(doc.type == 'zip') {
emit(doc.zip_code, doc)
}
}
I then added a bunch of docs related to zip codes, a sample doc goes like this;
{
"_id": "zip/48114",
"_rev": "1-990b2c4f682ed0b6a27e2fa0c066c93d",
"zip_code": 48114,
"state": null,
"county": null,
"rep_code1": "INTL2",
"rep_code2": "MI1",
"type": "zip"
}
Now when I query the view directly like so,
http://localhost:5984/partslocator/_design/partslocator/_view/zip?key=48114
I get the row back that I am expecting;
{
"total_rows": 41683,
"offset": 20391,
"rows": [
{
"id": "zip/48114",
"key": 48114,
"value": {
"_id": "zip/48114",
"_rev": "1-990b2c4f682ed0b6a27e2fa0c066c93d",
"zip_code": 48114,
"state": null,
"county": null,
"rep_code1": "INTL2",
"rep_code2": "MI1",
"type": "zip"
}
}
]
}
I have then set up a vhost and am using rewrites, and my rewrite for 'zip' looks like this.
{from: "/zip/:zip", to: "_view/zip", query: {"key": ":zip"}}
To me this seems like it should be correct, however when I try to query the view with the rewrite url, it always returns zero rows.
rewrite url:
http://partslocatordev.com:5984/zip/48114
response:
{
"total_rows": 41683,
"offset": 41683,
"rows": []
}
Am I missing anything here?
Note: I am using rewrites in the same fashion with other views and they work, but I cannot figure out why this one in particular isn't.
Upvotes: 2
Views: 255
Reputation: 885
It's likely that the rewriter is querying zip?key=":zip"
rather than zip?key=:zip
. You can use a formats
field in your rewriter to name how different arguments should be typed. In this case, try this:
{
from: "/zip/:zip",
to: "_view/zip",
query: {"key": ":zip"},
formats: {
"zip": "int"
}
}
Alternatively, in your map function, emit a string as the ID rather than a number, like this:
function (doc) {
if(doc.type == 'zip') {
emit(String(doc.zip_code), doc)
}
}
That will handle cases where the zipcode isn't an integer, like in the UK.
Upvotes: 2