Skywalker
Skywalker

Reputation: 5194

Passing values to a map function - CouchDB

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

Upvotes: 3

Views: 1494

Answers (1)

nlawson
nlawson

Reputation: 11620

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. :)

Upvotes: 5

Related Questions