According to the solutions proposed here I wrote this function to recursively scan keys in NodeJS Redis - node-redis for a given pattern:
RedisStore.prototype.scan = function(params, callback, cursor = '0', returnSet = new Set()) {
var self = this;
var options = {
pattern: '',
match: 'MATCH',
count: 100
};
for (var attrname in params) {
options[attrname] = params[attrname];
}
var count = '' + options.count;
self.client.scan(cursor, options.match, options.pattern, 'COUNT', count,
(err, reply) => {
if (err) {
return callback(err, null);
}
cursor = reply[0];
if (cursor === '0') { // scan completed
return callback(null, Array.from(returnSet));
} else {
var keys = reply[1];
keys.forEach(function(key, i) {
returnSet.add(key);
});
return self.scan(options, callback, cursor, returnSet);
}
});
} // scan
I have previously inserted a key with a prefix test: + some string, but scanning for
var res = await store.scan({
pattern: 'test:*',
count: 10
});
that will have as starting values cursor = '0' and returnSet = new Set(), does not seem to find anything, thus resulting in a empty resultSet and not reaching the 0 cursor exit condition. Why?