Skip to content

Commit

Permalink
fix(cursor): cursor hasNext returns false when exhausted
Browse files Browse the repository at this point in the history
Instead of throwing an error when the cursor is exhausted, the
hasNext method returns false.

Fixes NODE-1197
  • Loading branch information
rweinberger authored Jun 6, 2018
1 parent 783fe4b commit 184b817
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ const hasNext = (self, callback) => {
return callback(null, true);
}

if (self.isNotified()) {
return callback(null, false);
}

nextObject(self, function(err, doc) {
if (err) return callback(err, null);
if (self.s.state === Cursor.CLOSED || self.isDead()) return callback(null, false);
Expand Down
27 changes: 27 additions & 0 deletions test/functional/cursor_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4396,4 +4396,31 @@ describe('Cursor', function() {
cursor.close(() => client.close(() => done()));
});
});

it('should return false when exhausted and hasNext called more than once', function(done) {
const configuration = this.configuration;
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });

client.connect(function(err, client) {
const db = client.db(configuration.db);

db.createCollection('cursor_hasNext_test').then(function() {
const cursor = db.collection('cursor_hasNext_test').find();

cursor
.hasNext()
.then(function(val1) {
expect(val1).to.equal(false);
return cursor.hasNext();
})
.then(function(val2) {
expect(val2).to.equal(false);
cursor.close(() => client.close(() => done()));
})
.catch(err => {
cursor.close(() => client.close(() => done(err)));
});
});
});
});
});

0 comments on commit 184b817

Please sign in to comment.