Pregunta

I having problems using couchbase map functions with boolean key.

I wrote a map function with a boolean parameter, but when I try to use this function passing the value "false" as key the function returns nothing

Sample Document:

{
   "name": "lorem ipsum",
   "presentationId": "presentation_24e53b3a-db43-4d98-8499-3e8f3628a9c6",
   "fullPrice": 8,
   "isSold": false,
   "buyerId": null,
   "type": "ticket",
}

Map Function:

function(doc, meta) { 
     if (doc.type == "ticket" && doc.isSold && doc.presentationId) { 
         emit([doc.isSold, doc.presentationId], null); 
     } 
 }

http://localhost:8092/default/_design/tickets/_view/by_presentation_and_isSold?key=[false,"presentation_24e53b3a-db43-4d98-8499-3e8f3628a9c6"]

Result:

{"total_rows":10,"rows":[]}]}
¿Fue útil?

Solución

You are having this problem due to the check you do for the doc.isSold before the emit statement, the check means that only documents where doc.isSold == TRUE are passing through.

What you need to do is this which will check that the variable has been set rather than evaluating the boolean value:

function(doc, meta) { 
    if (doc.type == "ticket" && doc.isSold != null && doc.presentationId) { 
       emit([doc.isSold, doc.presentationId], null); 
    } 
}

Hope that helps :)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top