Chaining Asynchronous Callback Nodejs

344 views Asked by At

i m working on project with OrientDB so i m using gremlin graph language to implement some fonctionnality and i get some confuse about async callback chaining i want an instruction like this

var length = g.getEdges().count();

such us getEdges() is an async function

var grex = require('grex');
var async = require('async');
var gremlin = grex.gremlin;
var g = grex.g;
var client = grex.createClient(
{
    host: 'localhost',
    port: 8182,
    graph: 'orientdbsample'
}
);

function OrientGraph(obj) {
    this.result = obj.result;
    this.results = obj.results;

    this.getEdges = function(callback) {
        async.auto({
            getV: function(cb) {
                client.fetch(g.E(), function(err, response) {
                    if (err) return cb(err);
                    return cb(null, response);
                });
            }
        }, function(err, res) {
            callback(err, new OrientGraph({
                results: res.getV
            }));
        });
    }

    this.count = function(){
        return this.results.length;
    }
module.exports = OrientGraph; 
}

and in the Server.js file i use

var GremApi = require('./app/Api');
var og = new GremApi({
  results : []
});
og.getEdges(function(err,data){
  console.log(data.count());
}).count();

The Error is :

/home/xar/dev/stage/gremlin/server.js:28
}).count();
  ^
TypeError: Cannot read property 'count' of undefined
    at Object.<anonymous> (/home/xar/dev/stage/gremlin/server.js:28:3)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Function.Module.runMain (module.js:501:10)
    at startup (node.js:129:16)
    at node.js:814:3

Thank you

0

There are 0 answers