Actually, he is...

if doc.events is an object, then the object might not actually be copied, but only a reference.

If you change
var events = doc.events;
to
var events = clone doc.event;

Does that fix it?

On 20/04/2011 10:59 PM, Jan Lehnardt wrote:

On 20 Apr 2011, at 14:11, Nils Breunese wrote:

Hello all,

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.

This is an example of a map function which no longer works under 1.0.2:

----
function(doc) {
  if(doc.type === 'schedule') {
    var events = doc.events;
    if (events) {
      events.forEach(function(event) {
        var broadcasters = event.broadcasters;
        if (broadcasters) {
          broadcasters.forEach(function(broadcaster) {
            if(broadcaster === 'VPRO') {
              event.channel = doc.channel;
              event.channelName = doc.channelName;
              emit(event.end, event);
            }
          });
        }
      });
    }
  }
}
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 = {a:1};
js>  seal(a);
js>  a.a = 2;
typein:4: Error: a.a is read-only
js>  var b = a.a;
js>  print(b);
1
js>  b = 2
2
js>

Cheers
Jan

Reply via email to