mostlyserious / riak-js

Riak client for Javascript
http://riak-js.org
476 stars 102 forks source link

db.keys no longer works after re-install #189

Closed David-Broderick closed 11 years ago

David-Broderick commented 11 years ago

I reinstalled riak, node.js and riak-js on a new sever (Ubuntu 13.04) and suddenly when I use Riak-JS's db.keys() to get a list of keys in a bucket, it simply never returns.

Here's a test snippet that shows how it fails:

console.log('1');
db.save('bkt','x','1', function(err, data) {
    console.log('2');
    db.get('bkt','x', function(err, data) {
            console.log('3:'+data);
            db.keys('bkt', function(err, list) {
                    console.log('4');
                    for (key in list) { console.log('5:'+list[key]); }
            });
    });
});

Here's the output:

1

2

3:1

It doesn't output anything else. I installed the latest Riak (1.4.2), Node.js and Riak-JS (@latest).

Any ideas? Thanks!

sideshowcoder commented 11 years ago

Ok just a quick hint you can format code via `your code` triple ticks without the spaces, to make it a little more readable.

The problem with your code is that db.keys("bkt") returns a stream, which needs to be read first. So try something like this:

var buffer = [];
var keyStream = db.keys("bkt", {}, function(er, data, _meta){
  console.log(buffer)
});
keyStream.on("keys", function(keys) { buffer = buffer.concat(keys); });
keyStream.start();

which collects all the keys in the buffer array, the callback is called after the stream finishes so the buffer will contain all the keys.

David-Broderick commented 11 years ago

Thanks so much for your quick response! It works perfectly. And it makes much more sense to make it a stream, so I'm happy for the change.

Thanks again!

sideshowcoder commented 11 years ago

No problem, guess we can we close this then?