Couchdb requests a view with key parameters

Without a key parameter, the view works correctly

$curl "http://127.0.0.1:5984/music/_design/albums/_view/by_release_date"

 {"total_rows":311,"offset":0,"rows":[ {"id":"a4327d0718d3b1e227df7124a99a7fc3","key":"1991-12-22","value":{"by":"张楚","title":"黑月亮"}}, {"id":"a4327d0718d3b1e227df7124a99a3ac5","key":"unknown","value":{"by":"郑钧","title":"郑钧:赤裸裸"}}, 

but when with the key, I got either a bad query response or an empty result. Why?

$curl "http://127.0.0.1:5984/music/_design/albums/_view/by_release_date?key=unknown" {"Error": "bad_request", "reason": "invalid_json"}

$curl "http://127.0.0.1:5984/music/_design/albums/_view/by_release_date?key=1993" {"TOTAL_ROWS": 311, "offset": 0, "string": [

]}

Card Function:

 map function(doc) { key = doc.release_date value = {by: doc.author , title: doc.title} emit(key, value); } 
+8
json couchdb
source share
3 answers

The key is a string, so you need to include " = %22 , for example http://127.0.0.1:5984/music/_design/albums/_view/by_release_date?key=%221993%22

+34
source share

I assume that you are trying to request a range of keys. Try specifying startkey and endkey :

 http://127.0.0.1:5984/music/_design/albums/_view/by_release_date?startkey=1993&endkey=1993z 

More details: http://wiki.apache.org/couchdb/HTTP_view_API#Querying_Options

+1
source share

This is how I handle it

 let serverURL = "SERVER_IP:5984" let dd_name = 'email' // for example let viewname = "by_email" // for example const findDocByEmail = email => { let url = '${serverURL}/_design/${dd_name}/_view/${viewname}?key="${email}"' fetch(url, { method: 'GET', mode: "no-cors", headers:{ 'Content-Type': 'application/json', 'Authorization': 'Basic ' + btoa('COUCH_USER:SECRET'), // you can remove this if not needed, } }) .then(res => { return res.json() }) .then(response => console.log('Response: ', response)) .catch(error => console.log(error)) } 
0
source share

All Articles