Skip to content

Commit 184b817

Browse files
authored
fix(cursor): cursor hasNext returns false when exhausted
Instead of throwing an error when the cursor is exhausted, the hasNext method returns false. Fixes NODE-1197
1 parent 783fe4b commit 184b817

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

lib/cursor.js

+4
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ const hasNext = (self, callback) => {
237237
return callback(null, true);
238238
}
239239

240+
if (self.isNotified()) {
241+
return callback(null, false);
242+
}
243+
240244
nextObject(self, function(err, doc) {
241245
if (err) return callback(err, null);
242246
if (self.s.state === Cursor.CLOSED || self.isDead()) return callback(null, false);

test/functional/cursor_tests.js

+27
Original file line numberDiff line numberDiff line change
@@ -4396,4 +4396,31 @@ describe('Cursor', function() {
43964396
cursor.close(() => client.close(() => done()));
43974397
});
43984398
});
4399+
4400+
it('should return false when exhausted and hasNext called more than once', function(done) {
4401+
const configuration = this.configuration;
4402+
const client = configuration.newClient({ w: 1 }, { poolSize: 1, auto_reconnect: false });
4403+
4404+
client.connect(function(err, client) {
4405+
const db = client.db(configuration.db);
4406+
4407+
db.createCollection('cursor_hasNext_test').then(function() {
4408+
const cursor = db.collection('cursor_hasNext_test').find();
4409+
4410+
cursor
4411+
.hasNext()
4412+
.then(function(val1) {
4413+
expect(val1).to.equal(false);
4414+
return cursor.hasNext();
4415+
})
4416+
.then(function(val2) {
4417+
expect(val2).to.equal(false);
4418+
cursor.close(() => client.close(() => done()));
4419+
})
4420+
.catch(err => {
4421+
cursor.close(() => client.close(() => done(err)));
4422+
});
4423+
});
4424+
});
4425+
});
43994426
});

0 commit comments

Comments
 (0)