链接异步回调Nodejs

时间:2015-06-19 11:54:00

标签: javascript node.js asynchronous orientdb gremlin

我在使用OrientDB进行项目,所以我使用gremlin图形语言来实现一些功能,我对异步回调链接感到困惑,我想要这样的指令

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

这样我们的getEdges()是一个异步函数

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; 
}

并在Server.js文件中使用

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

错误是:

/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

谢谢

0 个答案:

没有答案