Search code examples
node.jsrethinkdbloopback

Return query rethinkdbdash on loopback remote method


1.- I'm working with loopback-connector-rethinkdbdash

2.- On a remote method I need to retrieve some random records from my DB, this is the code so far.

(function(){
    'use strict';
    module.exports = (Heatmap) => {
        var r = require('rethinkdb');
        // Connect to RethinkDB
        var p = r.connect({
            host: 'rethink',
            port: 28015,
            db: 'livedata'
        });


        // Error Handler
        function throwErr(err) {
            throw (err);
        }
        
        // Random Remote Method
        Heatmap.random = (cb) => {
            p.then(function(conn) {
                r.table('heatmap').run(conn, function(err, cursor) {
                        cursor.toArray(function(err, results) {
                            console.log('ALO-4', results)
                            cb(err, results);
                        })
                })
       	    }).error(throwErr);             
        }; // Heatmap.random

        Heatmap.remoteMethod(
            'random',
            {
              accepts : [],
              returns : { arg  : 'results', type : 'array', root : true },
              http    : { path : '/random', verb  : 'get' }
            }
        ); // Heatmap.remoteMethod
    };
}).call(this);

3.- I've already followed this documentation: https://github.com/neumino/rethinkdbdash https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Argumentdescriptions

4.- The thing is that the records or results return on the console.log('ALO-4') but they don't return in the browser...

I dunno what's going on, can someone help me?

Ty


Solution

  • Your code is a bit off, there you go:

    (function () {
    'use strict';
    module.exports = function (Heatmap) {
        var r = require('rethinkdb');
        // Connect to RethinkDB
        var p = r.connect({
            host: 'rethink',
            port: 28015,
            db: 'livedata'
        });
        // Error Handler
        function throwErr(err) {
            throw (err);
        }
        // Random Remote Method
        Heatmap.random = function (cb) {
            var results = null;
            p.then(function (conn) {
                r.table('heatmap').sample(100).run(conn, function (err, cursor) {
                    cursor.toArray(function (err, arr) {
                        results = arr;
                        cb(err, results);
                    });
                });
            }).error(throwErr);
        }; // Heatmap.random
        Heatmap.remoteMethod('random', {
            returns: { arg: 'results', type: 'array', root: true },
            http: { path: '/random', verb: 'get' }
        }); // Heatmap.remoteMethod
    };
    }).call(this);

    The error you mention happens because the data you return must have the same name as the "arg" you define below. On your code, "results" it's just raw data that doesn't has a name. Loopback's looking for something like:

    results = ["foo, bar, baz"];
    

    That's one thing. Another thing is that if you want random results (as one might think by the method's name), you need to provide the sample function, otherwise you're only returning all your data in the conventional (descending?) order and if the request it's big enough you might have issues.