[SERVER-150] Javascript closures Created: 12/Jul/09  Updated: 17/Feb/17  Resolved: 17/Feb/17

Status: Closed
Project: Core Server
Component/s: Shell
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Minor - P4
Reporter: sandstrom Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

OS X 10.5


Backwards Compatibility: Fully Compatible
Participants:

 Description   

If I'm not misstaken diff should be accessible within the keyf function, due to closures. However, I need to place it within the keyf function for the shell to recognise it. Is this a bug perhaps?

Result without diff within the keyf function:
------------------------------------------------------------------------------------------------
> getThanks('summaries', "759df69a-589e-41a2-b316-a6c91a6fe975")
Sun Jul 12 17:40:24 JS Error: uncaught exception:

{"errno" : -3 , "errmsg" : "invoke failed: JS Error: ReferenceError: diff is not defined nofile_b:4" , "ok" : 0}

Result with diff within the keyf function:
------------------------------------------------------------------------------------------------
> getThanks('summaries', "759df69a-589e-41a2-b316-a6c91a6fe975")
[

{"day" : 6 , "month" : 4 , "year" : 2009 , "count" : 0}

,

{"day" : 1 , "month" : 5 , "year" : 2009 , "count" : 0}

,

{"day" : 26 , "month" : 5 , "year" : 2009 , "count" : 0}

]

FUNCTION
------------------------------------------------------------------------------------------------
function getThanks(collection, id) {

var now = new Date();
var diff = 1000 * 60 * 60 * 24 * 30; // inaccessible

var res = db.group({
ns: collection,
// key:

{description:null}

,
keyf: function(a) {
var date = a.createdAt;
var time = date.getTime();
var now = new Date();
var diff = 1000 * 60 * 60 * 24 * 30; // accessible
// if (now.getTime() - time > diff)

{ // return null; // }

return

{ "day":date.getDate(), "month":date.getMonth(), "year":date.getFullYear() }

;
},
initial:

{count:0}

,
reduce: function(obj, prev)

{ // prev.abc.push(obj); prev.count + obj.thanks.length; }

,
cond:

{"user.$id": id }

,
});
// group() returns an array of grouped items. here, there will be a single
// item, as key is {}.
return res;
}



 Comments   
Comment by Mira Carey [ 17/Feb/17 ]

Making this change would be backwards breaking due to the extant server side environment (from sources like system.js) and as such we won't be able to consider this feature at this time.

Comment by Timo Mika Gläßer [ 19/Sep/10 ]

You should look into the documentation under "scope".

db.runCommand(

{ mapreduce : <collection>, map : <mapfunction>, reduce : <reducefunction> [, query : <query filter object>] [, sort : <sort the query. useful for optimization>] [, limit : <number of objects to return from collection>] [, out : <output-collection name>] [, keeptemp: <true|false>] [, finalize : <finalizefunction>] [, scope : <object where fields go into javascript global scope >] [, verbose : true] }

);

This can probably be used to "mimic" the behaviour you expect. Unfortunately the scope is not copied. I guess this has to do with shipping the functions or so?

Comment by sandstrom [ 13/Jul/09 ]

It's not in the documentation. I pasted everything into the shell and ran it.
I don't know if it's possible, but if it is thats something I would find very useful. Would definitely be nice to have, as it works in normal javascript environments.

Comment by Eliot Horowitz (Inactive) [ 13/Jul/09 ]

That would work if the function was being evaluated locally, but the closure isn't sent over automatically.
In babble it used to be, but not in the shell.
Is there documentation that still says it does?

Generated at Thu Feb 08 02:53:10 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.