Permalink
Browse files

fix for find, also calls callback when no keys are present

  • Loading branch information...
1 parent 37d58a4 commit 2671e5b93a5ae546c60b992ac7280d65537c2f1a mad voo committed with Tim Caswell Mar 13, 2011
Showing with 36 additions and 18 deletions.
  1. +23 −18 lib/nstore/query.js
  2. +13 −0 test/findAllTest.js
View
@@ -17,24 +17,29 @@ module.exports = function QueryPlugin() {
}
var keys = Object.keys(this.index);
var counter = keys.length;
- keys.forEach(function (key) {
- this.get(key, function (err, doc, key) {
- if (err) {
- if (stream) stream.emit('error', err);
- if (callback) callback(err);
- return;
- }
- if (filter(doc, key)) {
- if (stream) stream.emit('document', doc, key);
- if (callback) results[key] = doc;
- };
- counter--;
- if (counter === 0) {
- if (stream) stream.emit('end');
- if (callback) callback(null, results);
- }
- });
- }, this);
+ if(keys.length === 0) {
+ if (stream) stream.emit('end');
+ if (callback) callback(null, results);
+ } else {
+ keys.forEach(function (key) {
+ this.get(key, function (err, doc, key) {
+ if (err) {
+ if (stream) stream.emit('error', err);
+ if (callback) callback(err);
+ return;
+ }
+ if (filter(doc, key)) {
+ if (stream) stream.emit('document', doc, key);
+ if (callback) results[key] = doc;
+ };
+ counter--;
+ if (counter === 0) {
+ if (stream) stream.emit('end');
+ if (callback) callback(null, results);
+ }
+ });
+ }, this);
+ }
}
};
View
@@ -0,0 +1,13 @@
+require('./helper');
+nStore = nStore.extend(require('nstore/query')());
+
+
+expect("load");
+var store = nStore.new('fixtures/new.db', function () {
+ expect("all");
+ fulfill("load");
+ Step(
+ function() { store.all(this); },
+ function() { fulfill("all"); }
+ );
+});

0 comments on commit 2671e5b

Please sign in to comment.