jbmusso / grex

JavaScript graph database client for TinkerPop2 Rexster
MIT License
46 stars 12 forks source link

Error when using the grex library on a browser #23

Closed lvquoc closed 10 years ago

lvquoc commented 10 years ago

Hi Jean-Baptiste,

While working on my projects related to titan graph database, I found this useful grex library (https://github.com/gulthor/grex). I am looking at using that in my project. After downloading the source from github (https://github.com/gulthor/grex/archive/master.zip). I followed the instruction in the README file to include the grex.js script resided in the client directory in a html file as following:

        <script type="text/javascript" src="grex.js"></script>

Inside that HTML file, I have a very simple script as following:

        gRex.connect(options, function(err, g) {
            g.v(40088).both().get(function(err, result) {
                if (err) {
                    console.log(err);
                } else {
                    console.log(result);
                }
            })
        });

The above script doesn't work and it throws the error message on the browser console: "Uncaught ReferenceError: gRex is not defined ".

That error seems because the global gRex variable defined in the grex.min.js is not yet defined. Looking at the browser's console, I also saw another error "Uncaught TypeError: Object # has no method 'readFileSync' ". It seems to me that when loading up the grex.js file, it causes that TypeError and so making the gRex variable not defined. I also tried using the grex.min.js instead and still was the same error.

Can you please let me know what steps I should take to overcome that error so that I can use this grex library on the browser? Is there any details that I have missed?

Looking forward to your reply.

Many thanks,

Quoc

jbmusso commented 10 years ago

Hi Quoc,

Thank you for showing interest in gRex!

The current master branch isn't stable, it shouldn't work in the browser for two main reasons : there's a dependency (request, see package.json) that doesn't work in the browser and that I need to remove, and I didn't update the two built files (grex.js and grex.min.js). If you wish to try gRex in the browser, I suggest you download and experiment with v0.3.2 (https://github.com/gulthor/grex/tree/563a814eab374c5a0b3cda150c1c590747fcdd3b) : this should totally work in the browser.

The current master branch (a pre-v0.5.0, in some way) should only work in a Node.js environment, though you'll have trouble figuring out how it works because I didn't update the Readme.md file yet. Stable v0.5.0, which should be released soon, will work again in the browser.

With current master branch / v0.5.0, your code should read the following in a Node.js environment (note that .get() has been renamed to .exec()):

gRex.connect(options, function(err, client) {
  var gremlin = client.gremlin(); // instantiate a distinct gremlin class

  gremlin.g.v(40088).both().exec(function(err, result) {

    if (err) {
      console.log(err);
    } else {
      console.log(result);
    }
  })
});

Basically, you'll be manipulating instances of Gremlin classes which each represents a distinct query due for later execution. Each Gremlin instances provides convenient higher level methods which help you build a gremlin query (which can be accessed anytime as it is been built by looking at the gremlin.script property).

v0.5.0 allows you to handle multiline queries and, therefore, true transactions. The Rexster Batch kibble extension will no longer be required.

Let met know if you have more issues.