Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id D1A961114 for ; Wed, 20 Apr 2011 12:59:34 +0000 (UTC) Received: (qmail 68071 invoked by uid 500); 20 Apr 2011 12:59:31 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 68043 invoked by uid 500); 20 Apr 2011 12:59:31 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 68035 invoked by uid 99); 20 Apr 2011 12:59:31 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Apr 2011 12:59:31 +0000 X-ASF-Spam-Status: No, hits=0.7 required=5.0 tests=SPF_NEUTRAL X-Spam-Check-By: apache.org Received-SPF: neutral (athena.apache.org: local policy) Received: from [80.244.253.218] (HELO mail.traeumt.net) (80.244.253.218) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 20 Apr 2011 12:59:23 +0000 Received: from dahlia.local (p5795A99F.dip.t-dialin.net [87.149.169.159]) (using TLSv1 with cipher AES128-SHA (128/128 bits)) (No client certificate requested) by mail.traeumt.net (Postfix) with ESMTPSA id B78163C27C for ; Wed, 20 Apr 2011 14:59:01 +0200 (CEST) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Apple Message framework v1084) Subject: Re: Dealing with sealed docs From: Jan Lehnardt In-Reply-To: <6084E307-7CFB-4BAD-A12C-063272628B95@vpro.nl> Date: Wed, 20 Apr 2011 14:59:00 +0200 Content-Transfer-Encoding: quoted-printable Message-Id: <95333123-2B25-4E26-8C8A-D502C88D96B3@apache.org> References: <6084E307-7CFB-4BAD-A12C-063272628B95@vpro.nl> To: user@couchdb.apache.org X-Mailer: Apple Mail (2.1084) On 20 Apr 2011, at 14:11, Nils Breunese wrote: > Hello all, >=20 > I recently upgraded to CouchDB 1.0.2 for development and found some of = our view map functions broke. Apparently it's no longer possible to = modify data a doc in a view function before emitting. I found "Documents = are now sealed before being passed to map functions." in the changelog = for 1.0.2. >=20 > This is an example of a map function which no longer works under = 1.0.2: >=20 > ---- > function(doc) { > if(doc.type =3D=3D=3D 'schedule') { > var events =3D doc.events; > if (events) { > events.forEach(function(event) { > var broadcasters =3D event.broadcasters; > if (broadcasters) { > broadcasters.forEach(function(broadcaster) { > if(broadcaster =3D=3D=3D 'VPRO') { > event.channel =3D doc.channel; > event.channelName =3D doc.channelName; > emit(event.end, event); > } > }); > } > }); > } > } > } > ---- >=20 > A colleague suggested using this: >=20 > ---- > events.forEach(function(e) { > var event =3D eval(uneval(e)); > ---- >=20 > This creates a deep copy before modifying event properties. It works, = but it looks ugly to me. Is this the way to go or is there a cleaner = way? I use JSON.parse(JSON.stringify(doc)) but that is essentially the same = :) The reason that doc modifications work were an artefact of a bug in = Spidermonkey that finally got resolved. You shouldn't rely on being able = to modify a doc in map functions. That said, I'm surprised you get this error as you are not assigning = anything do doc.events. Unless I got wrong how chaining in JS works, = this shouldn't be a problem: js> var a =3D {a:1}; js> seal(a); js> a.a =3D 2; typein:4: Error: a.a is read-only js> var b =3D a.a; js> print(b); 1 js> b =3D 2 2 js>=20 Cheers Jan --=20