【问题标题】:Return query rethinkdbdash on loopback remote method在环回远程方法上返回查询 rethinkdbdash
【发布时间】:2016-07-07 23:53:07
【问题描述】:

1.- 我正在使用 loopback-connector-rethinkdbdash

2.- 在远程方法上,我需要从我的数据库中检索一些随机记录,这是目前为止的代码。

(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.- 我已经关注了这个文档:https://github.com/neumino/rethinkdbdash https://docs.strongloop.com/display/public/LB/Remote+methods#Remotemethods-Argumentdescriptions

4.- 问题是记录或结果会在 console.log('ALO-4') 上返回,但不会在浏览器中返回...

我不知道发生了什么,有人可以帮助我吗?

泰

【问题讨论】:

    标签: node.js rethinkdb loopback


    【解决方案1】:

    你的代码有点不对,你去吧:

    (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);

    您提到的错误发生是因为您返回的数据必须与您在下面定义的“arg”具有相同的名称。在您的代码中,“结果”只是没有名称的原始数据。 Loopback 正在寻找类似的东西:

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

    这是一回事。另一件事是,如果您想要随机结果(正如人们可能认为的方法名称),您需要提供示例函数,否则您只会以常规(降序?)顺序返回所有数据,如果请求它是足够大,您可能会遇到问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-25
      • 1970-01-01
      • 2013-08-15
      • 2018-05-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多