Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-150

Javascript closures

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Minor - P4 Minor - P4
    • None
    • Affects Version/s: None
    • Component/s: Shell
    • Labels:
      None
    • Environment:
      OS X 10.5
    • Fully Compatible

      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;
      }

            Assignee:
            backlog-server-platform DO NOT USE - Backlog - Platform Team
            Reporter:
            sandstrom sandstrom
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: