0

I have following function:

    function test(room_number, user_id) {
       return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, function(err, cursor){

        // I get the right result
        console.log(cursor);
       });
    }

How can I return this cursor back to the test function, so that I can call

test(2000, 1) and I get the result?

I only get this result:

{ _bitField: 1,
  _fulfillmentHandler0: [Function: successAdapter],
  _rejectionHandler0: [Function: errorAdapter],
  _progressHandler0: undefined,
  _promise0: [Function],
  _receiver0: [Circular],
  _settledValue: undefined }

I hope anybody can help me :)

markus_springer
  • 174
  • 1
  • 8
  • What test framework are you using? The call to the database is fundamentally asynchronous in node.js, so there's no way to return from the database while your test function is still running on the main thread. You need to have the `.run` callback invoke a success or failure callback provided by your testing framework. For example, mocha gives you a function called `done` that you can call – deontologician May 20 '15 at 02:09
  • I don't use any test framework because it's only a little project. Maybe the problem is that the connect query is a different scope than the function . – markus_springer May 20 '15 at 06:02
  • The issue is that you need a callback. The object you're getting as a result is called a promise, which is a way of organizing callbacks. There's no way to get an answer from the database before `test()` returns, because all I/O in node is done asynchronously. That means in order for the query results to be gathered back from the database, the test function has to return first. – deontologician May 20 '15 at 19:15
  • Try checking out this gist: https://gist.github.com/joakimbeng/8f57dae814a4802e2ae6 it's a small test runner you can copy/paste into your code, and will give you the ability to use callbacks to handle success. – deontologician May 20 '15 at 19:25

1 Answers1

0

This function is asynchronous and (because it's JavaScript) there's no way to make it synchronous. Hence, it's impossible for test(2000, 1) to return the cursor.

What you need to do instead is pass a callback and then test the result against the value provided by the callback. Keep in mind that in Node.js, by convention, we pass an error and a result to a callback function.

Hence, you would need to write your test like this:

function test(room_number, user_id, callback) {
       return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, callback);
    }

If you want the results as an array, you would have to to the following:

function test(room_number, user_id, callback) {
       return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, function(err, cursor) {
         if (err) callback(err, null);
         cursor.toArray(callback);
       };
    }

Now, when you run your test, you have to pass a callback with will compare what you expect the result to be, with the actual result.

test(2000, 1, function (err, cursor) { // If you use the first cursor
   // Convert cursor to array
   cursor.toArray(function (err, result) {
      // Compare `result` with expectations
      // Make sure that `result` is equal to [1, 2, 3];
      assert.deepEqual(result, [1, 2, 3]);
   });
})

Just to be clear, this is a JavaScript problem and has nothing to do specifically with how RethinkDB works. Because RethinkDB is executing a network call, that operation must be asynchronous.


Using Mocha

If you use Mocha as your testing framework, you can do the following.

it('should equal [1, 2, 3]', function (done) {
 return r.table('wins').filter({"room_number": room_number, "user_id" : user_id}).count().run(connection, function(err, cursor) {
   if (err) callback(err, null);
   cursor.toArray(function () {
     // Compare `result` with expectations
     // Make sure that `result` is equal to [1, 2, 3];
     assert.deepEqual(result, [1, 2, 3]);
     // Tell Mocha that you're done testing and 
     // want to continue to the next test
     done();
   });
};
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42