couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Anderson <jch...@apache.org>
Subject Re: small issue with a strange view
Date Fri, 08 Jan 2010 06:31:12 GMT
On Thu, Jan 7, 2010 at 5:43 PM, lenz <norbu09@googlemail.com> wrote:
> hi,
>
> i do stuff the clumsy way and overlook something completely obvious.
> my issue is that i try to map some very ugly API responses into a much
> saner data structure in a view. i did that in the first run simply
> this way:
>
> function(doc) {
>  emit(doc.domain,
>    {domain: doc.domain,
>     auth: doc.PROPERTY.AUTH[0],
>     created: doc.PROPERTY.CREATEDDATE[0],
>     expire: doc.PROPERTY.EXPIRATIONDATE[0],
>     ns: doc.PROPERTY.NAMESERVER,
>     status: doc.PROPERTY.STATUS,
>     renew: doc.PROPERTY.RENEWALMODE[0],
>     owner: {
>       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0],
>       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0],
>       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0],
>       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0],
>     .....
>
> then reality caught me and i noticed that some of the values i try to
> assign are actually not always there so i came up with this:
>
>      .......
>
>       firstname: doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME[0] || '',
>       lastname: doc.PROPERTY.OWNERCONTACT.PROPERTY.LASTNAME[0] ||
> doc.PROPERTY.OWNERCONTACT.PROPERTY.NAME[0],
>       company: doc.PROPERTY.OWNERCONTACT.PROPERTY.ORGANIZATION[0] || '',
>       street: doc.PROPERTY.OWNERCONTACT.PROPERTY.STREET[0] || '',
>      .......
>
> but obviously i get still errors as for example the
> doc.PROPERTY.OWNERCONTACT.PROPERTY.FIRSTNAME key is not defined at all
> and so the array i try to get the first element of is undefined and
> throws an error.
>
> any sane way i can do it nicely in the emit function or do i have to
> write a proper parser :-)
>

Shouldn't be too hard to write a function you can call with a path to
the data, that will stop digging if anything's missing:

lazyValue(doc, "foo.bar.BAM[3]")



> cheers
> lenz
>
> --
> iWantMyName.com
> painless domain registration (finally)
>



-- 
Chris Anderson
http://jchrisa.net
http://couch.io

Mime
View raw message