Passing values to a map function - CouchDB

依然范特西╮ 提交于 2019-12-11 02:34:00

问题


I was wondering whether its possible to pass values to a map function in couchDB design document.

For Example:

In the code below is it possible to pass a value that has been entered by the user and use that value to run the map function. Maybe I can pass users UserName when they login and then display the view based on the map function.

function(doc) {
   if(doc.name == data-Entered-By-User) {
   emit(doc.type, doc);
  }
}

Thank you in advance. Regards


回答1:


This is a common mistake in CouchDB when using views. It's kinda confusing, but instead of this:

function (doc) {
  if (doc.value === 'thing I am looking for') {
    emit(doc.value);
  }
}

What you want is this:

function (doc) {
  emit(doc.value);
}

And then when you query, you do:

/mydb/_design/myddoc/_view/myview?key="thing I am looking for"

You might want to read my 12 pro tips for better code with PouchDB, especially tip #9. The tips apply equally well to CouchDB. :)



来源:https://stackoverflow.com/questions/28572449/passing-values-to-a-map-function-couchdb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!