0

I am not sure is it possible or not. I need to use a t-distribution in my ReQL from a nodeJS Package. The code is something like this:

    Query.map(function(doc){
      return {
               'DesScore': r.do(doc('MXt'),
                                doc('MXdf'), 
                                r.js('(function(v,df){
                                       var distributions = require("distributions.js");
                                       var studentt = distributions.Studentt(df);
                                       return studentt.inv(-1.*Math.abs(v))*2.*100.;}) '))
             };
    })

But I get

Unhandled rejection ReqlQueryLogicError: ReferenceError: require is not defined in:

How can I load a package in ReQL js interpreter?

Thanks in advance for any help.

  • What's `r` (#doc, #js, etc.)? – Tejas Manohar Nov 09 '15 at 10:39
  • r is rethinkDB namespace – Roozbeh Pazuki Nov 09 '15 at 10:58
  • Are you intending to pass in a function as a string here? Don't quite see what you're doing here, but I'm not super familiar w/ Rethink itself. Maybe add some background info? – Tejas Manohar Nov 09 '15 at 13:20
  • I don't know any other way to call a js function in ReQL except the above one. There, I passed `doc('MXt')` and `doc('MXdf')` as the arguments of the js function( I assumed these two are parts of the `Query` which are passed to `map` function). Inside the js function, I need to call a method from and external nodejs' package and as you can see in the error message, the js interpreter does not have any idea about it. I'm wondering how can I load that package? – Roozbeh Pazuki Nov 09 '15 at 17:09

1 Answers1

0

You cannot use require in r.js because it's JavaScript v8, not NodeJS. require is come from NodeJS API, it isn't in standard JavaScript. On JavaScript client side, people use browserify (or friend) for that purpose. So to use require, you have to implement a shim for require inside r.js, which may also pull it other dependencies...

More than that, you also have to somehow distribute your dependencies to RethinkDB server itself for require to work.

All of those are a mess. So probably try to find another solution.

Think of r.js as the last way to do something, and try to use native ReQL whenever possible, as the document say:

https://www.rethinkdb.com/api/javascript/js/

With control structure like forEeach, do, branch I think you can do pretty much stuff with it.

kureikain
  • 2,304
  • 2
  • 14
  • 9
  • Thanks a lot @kureikain. I don't know how to write t-distribution and/or gamma function etc. by ReQL (which is what I wanted to load from nodejs package) while ReQL even does not support a simple sqrt. I think rethinkDB requires UDF feature for such scenarios. As much as I try rethinkDB, I understand it is not mature yet. – Roozbeh Pazuki Nov 10 '15 at 08:57