There are a couple of ways you could go about this. Keep in mind that these would only work if you're not currently in changefeed.
1. Using cursor.toArray()
You could first convert the cursor into an array and then just use the index to determine if there is another row on the cursor.
r
.table('hello')
.filter({ name: 'jorge' })
.run(conn)
.then(function (cursor) {
return cursor.toArray();
})
.then(function (array) {
array.forEach(function (row, i) {
if (i === array.length - 1) {
// There are now more rows after this
} else {
// There are more rows
}
});
});
As coffeemug mentioned above, this approach won't work for large datasets since converting the cursor into an array means that you have to load all the data.
2. Using cursor.next
A less convenient way of doing it, but probably more performant is to use cursor.next
. The way this would work is that the .next
method on the cursor would throw an error if there are no more rows in the cursor.
Your code would look something like this:
r
.table('hello')
.filter({ name: 'jorge' })
.run(conn)
.then(function (cursor) {
var hasNextRow = function (cb, prevRow) {
cursor.next(function (err, row) {
if (err) cb(false, prevRow);
if (row) cb(true, prevRow, row);
})
};
var consoleRow = function (row) {
hasNextRow(function (hasNextRow, prevRow, row) {
if (prevRow) {
if (!hasNextRow) console.log('isLast');
// This is your row
console.log(i, prevRow);
}
if (hasNextRow) {
consoleRow(row);
}
}, row);
};
consoleRow();
});
Basically, this code saves a reference to the last row and goes on to the next row. At that point, it'll know if the next row exists or not and you can handle any behavior on that row using the prevRow
variable.