Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't pass a closure to db.query #1264

Closed
espringe opened this issue Jan 29, 2014 · 11 comments
Closed

Can't pass a closure to db.query #1264

espringe opened this issue Jan 29, 2014 · 11 comments

Comments

@espringe
Copy link

Example:

function f(what) {
  function map(doc) {
      if (doc.type === what)
          emit(doc._id, doc);
  }

  Database.query(map, function(){});
}

f('account')

Gives: Uncaught ReferenceError: what is not defined

@calvinmetcalf
Copy link
Member

yes this is a known error, there is no way to get emit to be in scope without destroying the closure, though that gives me an idea

@espringe
Copy link
Author

FWIW this works:

new Function('doc',
  'if (doc.type === ' + JSON.stringify(what) + ')' +
     'emit(doc._id, doc);'
);

But it makes me cry a little bit

@espringe
Copy link
Author

there is no way to get emit to be in scope without destroying the closure

Does emit really need to be in scope? I'd prefer taking the emitter as an argument to the function .. or if that's too verbose, even using this.emit() ... as it's a lot less magical (And you don't need to smash the closure)

@calvinmetcalf
Copy link
Member

I'm literally working on a pull to map reduce that takes emit as the second
argument

On Wed, Jan 29, 2014 at 2:15 PM, Eric Springer notifications@github.comwrote:

there is no way to get emit to be in scope without destroying the closure

Does emit really need to be in scope? I'd prefer taking the emitter as an
argument to the function .. or if that's too verbose, even using
this.emit() ... as it's a lot less magical (And you don't need to smash
the closure)

Reply to this email directly or view it on GitHubhttps://github.com/daleharvey/pouchdb/issues/1264#issuecomment-33619135
.

-Calvin W. Metcalf

@chesles
Copy link
Contributor

chesles commented Jan 29, 2014

Only problem with that is it breaks compatibility with couchdb, so you can't use the same map function as you do in couch (e.g. in a design doc) because emit is in scope in couch, but having an argument named emit would shadow that. It's an unfortunate situation, to be sure.

@calvinmetcalf
Copy link
Member

@chesles this only applies to the temp views, not the design doc views, as they are stringified anyway so they have to be evaled

@chesles
Copy link
Contributor

chesles commented Jan 29, 2014

Ah, nice. That's awesome. +1 :)

@calvinmetcalf
Copy link
Member

fyi I do have a pull for couchdb to get this syntax in apache/couchdb#127

@nolanlawson
Copy link
Member

The new feature is merged in pouchdb/mapreduce. If you can't wait for the next release and want to try the bleeding edge, I've built it here: pouchdb-nightly.js and pouchdb-nightly.min.js.

var pouch = new PouchDB('mydb');
pouch.post({foo:'bar'})
.then(function(){
  var myFoo = 'bar';
  return pouch.query(function(doc, emit){
    if (doc.foo === myFoo){
      emit('it works!');
    }
  });
})
.then(function(result){
    console.log(result.rows[0].key); // 'it works!'
});

Oh yeah, did I mention the next version of Pouch will have promises?! @calvinmetcalf has been tearing it up recently.

@nolanlawson
Copy link
Member

Documentation here btw.

@wspringer
Copy link

Awesome. By the way, my angular-pouchdb wrapper uses Angular's $q promise, if anyone is interested.

calvinmetcalf added a commit that referenced this issue Mar 8, 2014
(#1264 pouchdb/mapreduce#37) - document eval avoidance
nolanlawson added a commit that referenced this issue Mar 8, 2014
(#1264 pouchdb/mapreduce#37) - document eval avoidance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants