开发者

CouchDB view question - how to return "" (empty string) instead of null?

开发者 https://www.devze.com 2023-02-17 22:38 出处:网络
I have a CouchDB view from which I emit three or four fields. If the PHONE_MOBILE field is empty for a given record, the view output contains null

I have a CouchDB view from which I emit three or four fields.

If the PHONE_MOBILE field is empty for a given record, the view output contains null

Instead I'd like it to emit "" (i.e. blank string/nothing)

What's the best way to achieve this? Here's the view code:

{
   "_id": "_design/blah",
   "_rev": "20-e07e50de179d0df5e7bce52fdb7ee4d2",
   "views": {
       "by_surname3": {
           "map": "function(doc) { if (doc.SURNAME)  emit(doc.SURNAME.toLowerCase(), {SURN开发者_C百科AME: doc.SURNAME, FIRSTNAME: doc.FIRSTNAME, PHONE_MOBILE: doc.PHONE_MOBILE}) }"
       }
   }
}

Thanks


You can use something like this:

function(doc) { 
  if (doc.SURNAME)  
    emit(doc.SURNAME.toLowerCase(), {
        SURNAME: doc.SURNAME, 
        FIRSTNAME: doc.FIRSTNAME, 
        PHONE_MOBILE: (doc.PHONE_MOBILE ? doc.PHONE_MOBILE : "")
    }) 
}

Or, if you prefer, the or operator to provide a default.

function(doc) { 
  if (doc.SURNAME)  
    emit(doc.SURNAME.toLowerCase(), {
        SURNAME: doc.SURNAME, 
        FIRSTNAME: doc.FIRSTNAME, 
        PHONE_MOBILE: (doc.PHONE_MOBILE || "")
    }) 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消