Array mutating after scope change?

37 views
Skip to first unread message

kuno

unread,
Jun 26, 2012, 5:43:26 AM6/26/12
to nod...@googlegroups.com
This error occurs on node 0.6.19, not sure be fixed on 0.8 or not.

suppose I have a nest function like this:
<=======================================
function outer() {
var query = {};
query.startkey = [];
query.endkey = [];

query.startkey.push('report');
query.startkey.push({});

var keysCallback =  function(result) {
     query.startkey.push(result.key1);
     query.startkey.push(result.key2);
      ......
}

getResultFunction(args, keysCallback);
}
======================================>
most time, it works well, but in some very very rare chance, I got the error like this:

<=======================================
Object ["report",{}] has no method 'push'
========================================>
if we inspect it, it'll tell that query.startkeys is an object with the array literal notation '["report",{}]', but not a real array.

Since it occurs inside the inner keysCallback, but query.startkey was defined explicitly in the outer function, and works well.
This make methink maybe there are some runtime errors, during the runtime lookup the query.startkeys from the scope of keysCallback to the outer function.

But I am not a expert on v8, so this is just a suspection. I'd like to hear your opinion about this.

--thanks

kuno 

kuno

unread,
Jun 26, 2012, 5:51:18 AM6/26/12
to nod...@googlegroups.com
Sorry, missing something important.

This error occurs line no.10, i.e 
<================================
query.startkey.push(result.key1);
=================================> 

Bradley Meck

unread,
Jun 26, 2012, 8:34:46 AM6/26/12
to nod...@googlegroups.com
It would be helpful to see some more code, for example, is `query` ever exposed in the outside scope? Where is it referenced for usage rather than population?

arun antony

unread,
Jun 26, 2012, 9:31:06 AM6/26/12
to nod...@googlegroups.com
Hi
 
please try
 
make the variable named query to global, otherwise put that variable(query) declaration into the function named keysCallback
 
Regards
Arun Antony


--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

kuno

unread,
Jun 26, 2012, 1:35:26 PM6/26/12
to nod...@googlegroups.com
I agree, but since this is a private project of our company, I am afraid I'm not allowed to show the real code.
 
Reply all
Reply to author
Forward
0 new messages