CouchDB
  1. CouchDB
  2. COUCHDB-1202

Function 'require' undefined in reduce functions, cannot require CommonJS module

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 1.1
    • Fix Version/s: None
    • Component/s: JavaScript View Server
    • Environment:

      Mac OS X 10.6.8, CouchDB 1.1.0 (homebrew), Erlang R14B03

    • Skill Level:
      Regular Contributors Level (Easy to Medium)

      Description

      CouchDB 1.1.0 views can require CommonJS modules according to the Modules 1.1.1 specification when defined in the design document under "views/lib". In CouchDB 1.1.0 this works in map functions, but the "require" is undefined in reduce functions. See the shell transcript along with the design document and log output. Also available at [1].

      ddoc.json:
      {
      "views": {
      "lib":

      { "foo": "exports.bar = 42;" }

      ,
      "test": {
      "map": "function(doc)

      { emit(doc._id, require('views/lib/foo').bar); }

      ",
      "reduce": "function (keys, values, rereduce)

      { return require('views/lib/foo').bar; }

      "
      }
      }
      }

      require_test.sh:

      $ curl http://localhost:5984

      {"couchdb":"Welcome","version":"1.1.0"}

      $ curl -X PUT http://localhost:5984/reduce_test

      {"ok":true}

      $ curl -X PUT http://localhost:5984/reduce_test/_design/test --data @ddoc.json

      {"ok":true,"id":"_design/test","rev":"1-5a587aeb82027657db334265037a4692"}

      $ curl -X POST http://localhost:5984/reduce_test -H "Content-Type: application/json" -d '{}'

      {"ok":true,"id":"cd5869297d813d45d3e9f8016f001834","rev":"1-967a00dff5e02add41819138abb3284d"}

      $ curl "http://localhost:5984/reduce_test/_design/test/_view/test?reduce=false"
      {"total_rows":1,"offset":0,"rows":[

      {"id":"cd5869297d813d45d3e9f8016f001834","key":"cd5869297d813d45d3e9f8016f001834","value":42}

      ]}

      $ curl "http://localhost:5984/reduce_test/_design/test/_view/test"
      {"rows":[

      {"key":null,"value":null}

      ]}

      ^^^ Expected

      {"key":null,"value":42}

      couch.log:

      [info] [<0.313.0>] OS Process #Port<0.2368> Log :: function raised exception (new ReferenceError("require is not defined", ""))
      [info] [<0.14096.0>] 127.0.0.1 - - 'GET' /reduce_test/_design/test/_view/test 200

      [1] https://gist.github.com/1049231

      1. reduce_lib.patch
        13 kB
        Dipesh Patel

        Activity

        Hide
        Dipesh Patel added a comment -

        Hello, I've attempted a patch for this. Please find attached.

        You can also find it on github here:

        https://github.com/dipthegeezer/couchdb/tree/1202-require-undefined-in-reduce

        Let me know if there are any issues with it.

        Dip

        Show
        Dipesh Patel added a comment - Hello, I've attempted a patch for this. Please find attached. You can also find it on github here: https://github.com/dipthegeezer/couchdb/tree/1202-require-undefined-in-reduce Let me know if there are any issues with it. Dip
        Hide
        James Dingwall added a comment -

        The ability to require() in reduce functions still seems to be broken in CouchDB 1.5.0. Is this an oversight of this bug or is there a good reason not to support require() in reduce and this should be closed as won't fix?

        [Mon, 17 Mar 2014 13:27:51 GMT] [info] [<0.5226.0>] OS Process #Port<0.7704> Log :: function raised exception (new ReferenceError("require is not defined", "undefined", 11))

        Show
        James Dingwall added a comment - The ability to require() in reduce functions still seems to be broken in CouchDB 1.5.0. Is this an oversight of this bug or is there a good reason not to support require() in reduce and this should be closed as won't fix? [Mon, 17 Mar 2014 13:27:51 GMT] [info] [<0.5226.0>] OS Process #Port<0.7704> Log :: function raised exception (new ReferenceError("require is not defined", "undefined", 11))
        Hide
        Alexander Shorin added a comment -

        Not with current query server protocol, but we have plans to improve it.

        Show
        Alexander Shorin added a comment - Not with current query server protocol, but we have plans to improve it.

          People

          • Assignee:
            Unassigned
            Reporter:
            Gabor Ratky
          • Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated:

              Development